Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. import cv2, datetime, pandas
  2.  
  3. first_frame = None
  4. #status_list = [None, None]
  5. #times = []
  6. #df = pandas.DataFrame(columns=["Start", "End"])
  7.  
  8. video = cv2.VideoCapture(0)
  9. while True:
  10. check, frame = video.read()
  11. #status = 0
  12. gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
  13. gray = cv2.GaussianBlur(gray, (21, 21), 0)
  14. if first_frame is None:
  15. first_frame = gray
  16. continue
  17. delta_frame = cv2.absdiff(first_frame, gray) # calculates the difference between the first and other frames
  18. thresh_delta = cv2.threshold(delta_frame, 30, 255, cv2.THRESH_BINARY)[1] # threshold value, if difference > 30, it will convert pixels to white
  19. thresh_delta = cv2.dilate(thresh_delta, None, iterations=0)
  20. cnts, B = cv2.findContours(thresh_delta.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
  21. for contour in cnts:
  22. if cv2.contourArea(contour) < 1000: # removes noises and shadows (< 1000 pixels)
  23. continue
  24. status = 1
  25. (x, y, w, h) = cv2.boundingRect(contour) # box around the object
  26. cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 3)
  27.  
  28. #status_list.append(status)
  29. #status_list = status_list[-2:]
  30.  
  31. #if status_list[-1] == 1 and status_list[-2] ==0:
  32. # times.append(datetime.datetime.now())
  33. #if status_list[-1] == 0 and status_list[-2] == 1:
  34. # times.append(datetime.datetime.now())
  35. #print(status_list)
  36. #print(times)
  37. # for i in range(0,len(times),2):
  38. # df = df.append({"Start": times[i], "End": times[i+1]}, ignore_index=True)
  39. # print(df)
  40. cv2.imshow('frame', frame)
  41. cv2.imshow('Capturing', gray)
  42. cv2.imshow('delta', delta_frame)
  43. cv2.imshow('thresh', thresh_delta)
  44. key = cv2.waitKey(1)
  45. if key == ord('q'):
  46. break
  47. video.release()
  48. cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement