take_picture.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import cv2
  2. import time
  3. key = cv2.waitKey(1)
  4. webcam = cv2.VideoCapture(0)
  5. while True:
  6. try:
  7. check, frame = webcam.read()
  8. # print(check) # prints true as long as the webcam is running
  9. # print(frame) # prints matrix values of each framecd
  10. cv2.imshow("Capturing", frame)
  11. key = cv2.waitKey(1)
  12. if key == ord('s'):
  13. ts = time.time()
  14. cv2.imwrite(filename='saved_img-{}.jpg'.format(ts), img=frame)
  15. # webcam.release()
  16. # img_new = cv2.imread('saved_img.jpg', cv2.IMREAD_GRAYSCALE)
  17. # img_new = cv2.imshow("Captured Image", img_new)
  18. cv2.waitKey(500)
  19. # cv2.destroyAllWindows()
  20. # print("Processing image...")
  21. # img_ = cv2.imread('saved_img.jpg', cv2.IMREAD_ANYCOLOR)
  22. # print("Converting RGB image to grayscale...")
  23. # gray = cv2.cvtColor(img_, cv2.COLOR_BGR2GRAY)
  24. # print("Converted RGB image to grayscale...")
  25. # print("Resizing image to 28x28 scale...")
  26. # img_ = cv2.resize(gray, (28, 28))
  27. # print("Resized...")
  28. # img_resized = cv2.imwrite(filename='saved_img-final.jpg', img=img_)
  29. print("Image saved!")
  30. # break
  31. elif key == ord('q'):
  32. print("Turning off camera.")
  33. webcam.release()
  34. print("Camera off.")
  35. print("Program ended.")
  36. cv2.destroyAllWindows()
  37. break
  38. except(KeyboardInterrupt):
  39. print("Turning off camera.")
  40. webcam.release()
  41. print("Camera off.")
  42. print("Program ended.")
  43. cv2.destroyAllWindows()
  44. break