take_picture_snn.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. # Establish a connection to the webcam
  2. import uuid
  3. import cv2
  4. import os
  5. # Setup paths
  6. POS_PATH = os.path.join('data', 'positive')
  7. NEG_PATH = os.path.join('data', 'negative')
  8. ANC_PATH = os.path.join('data', 'anchor')
  9. cap = cv2.VideoCapture(0)
  10. CAMERA_OFFSET_X = 750
  11. CAMERA_OFFSET_Y = 250
  12. CAMERA_SIZE = 500
  13. FEED_SIZE = 250
  14. while cap.isOpened():
  15. ret, frame = cap.read()
  16. # Cut down frame to 250x250px
  17. frame = frame[CAMERA_OFFSET_Y:CAMERA_OFFSET_Y + CAMERA_SIZE, CAMERA_OFFSET_X:CAMERA_OFFSET_X + CAMERA_SIZE, :]
  18. # Collect anchors
  19. if cv2.waitKey(1) & 0XFF == ord('a'):
  20. # Create the unique file path
  21. imgname = os.path.join(ANC_PATH, '{}.jpg'.format(uuid.uuid1()))
  22. # Resize image
  23. resized = cv2.resize(frame, (FEED_SIZE, FEED_SIZE))
  24. # Write out anchor image
  25. cv2.imwrite(imgname, resized)
  26. # Collect positives
  27. if cv2.waitKey(1) & 0XFF == ord('p'):
  28. # Create the unique file path
  29. imgname = os.path.join(POS_PATH, '{}.jpg'.format(uuid.uuid1()))
  30. # Resize image
  31. resized = cv2.resize(frame, (FEED_SIZE, FEED_SIZE))
  32. # Write out positive image
  33. cv2.imwrite(imgname, resized)
  34. # Show image back to screen
  35. cv2.imshow('Image Collection', frame)
  36. # Breaking gracefully
  37. if cv2.waitKey(1) & 0XFF == ord('q'):
  38. break
  39. # Release the webcam
  40. cap.release()
  41. # Close the image show frame
  42. cv2.destroyAllWindows()