take_picture_snn.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. NAME = "kevin"
  15. while cap.isOpened():
  16. ret, frame = cap.read()
  17. # Cut down frame to 250x250px
  18. frame = frame[CAMERA_OFFSET_Y:CAMERA_OFFSET_Y + CAMERA_SIZE, CAMERA_OFFSET_X:CAMERA_OFFSET_X + CAMERA_SIZE, :]
  19. # Collect anchors
  20. if cv2.waitKey(1) & 0XFF == ord('a'):
  21. # Create the unique file path
  22. imgname = os.path.join(ANC_PATH, '{}-{}.jpg'.format(NAME,uuid.uuid1()))
  23. # Resize image
  24. resized = cv2.resize(frame, (FEED_SIZE, FEED_SIZE))
  25. # Write out anchor image
  26. cv2.imwrite(imgname, resized)
  27. # Collect positives
  28. if cv2.waitKey(1) & 0XFF == ord('p'):
  29. # Create the unique file path
  30. imgname = os.path.join(POS_PATH, '{}-{}.jpg'.format(NAME,uuid.uuid1()))
  31. # Resize image
  32. resized = cv2.resize(frame, (FEED_SIZE, FEED_SIZE))
  33. # Write out positive image
  34. cv2.imwrite(imgname, resized)
  35. # Show image back to screen
  36. cv2.imshow('Image Collection', frame)
  37. # Breaking gracefully
  38. if cv2.waitKey(1) & 0XFF == ord('q'):
  39. break
  40. # Release the webcam
  41. cap.release()
  42. # Close the image show frame
  43. cv2.destroyAllWindows()