12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- # Establish a connection to the webcam
- import uuid
- import cv2
- import os
- # Setup paths
- POS_PATH = os.path.join('data', 'positive')
- NEG_PATH = os.path.join('data', 'negative')
- ANC_PATH = os.path.join('data', 'anchor')
- cap = cv2.VideoCapture(0)
- CAMERA_OFFSET_X = 750
- CAMERA_OFFSET_Y = 250
- CAMERA_SIZE = 500
- FEED_SIZE = 250
- NAME = "kevin"
- while cap.isOpened():
- ret, frame = cap.read()
- # Cut down frame to 250x250px
- frame = frame[CAMERA_OFFSET_Y:CAMERA_OFFSET_Y + CAMERA_SIZE, CAMERA_OFFSET_X:CAMERA_OFFSET_X + CAMERA_SIZE, :]
- # Collect anchors
- if cv2.waitKey(1) & 0XFF == ord('a'):
- # Create the unique file path
- imgname = os.path.join(ANC_PATH, '{}-{}.jpg'.format(NAME,uuid.uuid1()))
- # Resize image
- resized = cv2.resize(frame, (FEED_SIZE, FEED_SIZE))
- # Write out anchor image
- cv2.imwrite(imgname, resized)
- # Collect positives
- if cv2.waitKey(1) & 0XFF == ord('p'):
- # Create the unique file path
- imgname = os.path.join(POS_PATH, '{}-{}.jpg'.format(NAME,uuid.uuid1()))
- # Resize image
- resized = cv2.resize(frame, (FEED_SIZE, FEED_SIZE))
- # Write out positive image
- cv2.imwrite(imgname, resized)
- # Show image back to screen
- cv2.imshow('Image Collection', frame)
- # Breaking gracefully
- if cv2.waitKey(1) & 0XFF == ord('q'):
- break
- # Release the webcam
- cap.release()
- # Close the image show frame
- cv2.destroyAllWindows()
|