Browse Source

take picture

kevin 3 năm trước cách đây
mục cha
commit
e07f518a5c
1 tập tin đã thay đổi với 47 bổ sung0 xóa
  1. 47 0
      take_picture.py

+ 47 - 0
take_picture.py

@@ -0,0 +1,47 @@
+import cv2
+import time
+
+key = cv2.waitKey(1)
+webcam = cv2.VideoCapture(0)
+while True:
+    try:
+        check, frame = webcam.read()
+        print(check)  # prints true as long as the webcam is running
+        print(frame)  # prints matrix values of each framecd
+        cv2.imshow("Capturing", frame)
+        key = cv2.waitKey(1)
+        if key == ord('s'):
+            ts = time.time()
+            cv2.imwrite(filename='saved_img-{}.jpg'.format(ts), img=frame)
+            # webcam.release()
+            # img_new = cv2.imread('saved_img.jpg', cv2.IMREAD_GRAYSCALE)
+            # img_new = cv2.imshow("Captured Image", img_new)
+            cv2.waitKey(500)
+            # cv2.destroyAllWindows()
+            # print("Processing image...")
+            # img_ = cv2.imread('saved_img.jpg', cv2.IMREAD_ANYCOLOR)
+            # print("Converting RGB image to grayscale...")
+            # gray = cv2.cvtColor(img_, cv2.COLOR_BGR2GRAY)
+            # print("Converted RGB image to grayscale...")
+            # print("Resizing image to 28x28 scale...")
+            # img_ = cv2.resize(gray, (28, 28))
+            # print("Resized...")
+            # img_resized = cv2.imwrite(filename='saved_img-final.jpg', img=img_)
+            print("Image saved!")
+
+            # break
+        elif key == ord('q'):
+            print("Turning off camera.")
+            webcam.release()
+            print("Camera off.")
+            print("Program ended.")
+            cv2.destroyAllWindows()
+            break
+
+    except(KeyboardInterrupt):
+        print("Turning off camera.")
+        webcam.release()
+        print("Camera off.")
+        print("Program ended.")
+        cv2.destroyAllWindows()
+        break