Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 79a79b3

Browse files
Handled KeyboardInterrupt exception.
1 parent e53a63f commit 79a79b3

File tree

2 files changed

+68
-49
lines changed

2 files changed

+68
-49
lines changed

‎for-windows-users/webcam-capture-v1.0.1-windows.py

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,38 @@
44
webcam = cv2.VideoCapture(0)
55
sleep(2)
66
while True:
7-
8-
check, frame = webcam.read()
9-
print(check) #prints true as long as the webcam is running
10-
print(frame) #prints matrix values of each framecd
11-
cv2.imshow("Capturing", frame)
12-
key = cv2.waitKey(1)
13-
if key == ord('s'):
14-
cv2.imwrite(filename='saved_img.jpg', img=frame)
15-
webcam.release()
16-
print("Processing image...")
17-
img_ = cv2.imread('saved_img.jpg', cv2.IMREAD_ANYCOLOR)
18-
print("Converting RGB image to grayscale...")
19-
gray = cv2.cvtColor(img_, cv2.COLOR_BGR2GRAY)
20-
print("Converted RGB image to grayscale...")
21-
print("Resizing image to 28x28 scale...")
22-
img_ = cv2.resize(gray,(28,28))
23-
print("Resized...")
24-
img_resized = cv2.imwrite(filename='saved_img-final.jpg', img=img_)
25-
print("Image saved!")
26-
27-
break
28-
elif key == ord('q'):
7+
8+
try:
9+
check, frame = webcam.read()
10+
print(check) #prints true as long as the webcam is running
11+
print(frame) #prints matrix values of each framecd
12+
cv2.imshow("Capturing", frame)
13+
key = cv2.waitKey(1)
14+
if key == ord('s'):
15+
cv2.imwrite(filename='saved_img.jpg', img=frame)
16+
webcam.release()
17+
print("Processing image...")
18+
img_ = cv2.imread('saved_img.jpg', cv2.IMREAD_ANYCOLOR)
19+
print("Converting RGB image to grayscale...")
20+
gray = cv2.cvtColor(img_, cv2.COLOR_BGR2GRAY)
21+
print("Converted RGB image to grayscale...")
22+
print("Resizing image to 28x28 scale...")
23+
img_ = cv2.resize(gray,(28,28))
24+
print("Resized...")
25+
img_resized = cv2.imwrite(filename='saved_img-final.jpg', img=img_)
26+
print("Image saved!")
27+
28+
break
29+
30+
elif key == ord('q'):
31+
webcam.release()
32+
cv2.destroyAllWindows()
33+
break
34+
35+
except(KeyboardInterrupt):
36+
print("Turning off camera.")
2937
webcam.release()
38+
print("Camera off.")
39+
print("Program ended.")
3040
cv2.destroyAllWindows()
31-
break
41+
break

‎webcam-capture-v1.01.py

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,44 @@
33
key = cv2. waitKey(1)
44
webcam = cv2.VideoCapture(0)
55
while True:
6-
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-
cv2.imwrite(filename='saved_img.jpg', img=frame)
14-
webcam.release()
15-
img_new = cv2.imread('saved_img.jpg', cv2.IMREAD_GRAYSCALE)
16-
img_new = cv2.imshow("Captured Image", img_new)
17-
cv2.waitKey(1650)
18-
cv2.destroyAllWindows()
19-
print("Processing image...")
20-
img_ = cv2.imread('saved_img.jpg', cv2.IMREAD_ANYCOLOR)
21-
print("Converting RGB image to grayscale...")
22-
gray = cv2.cvtColor(img_, cv2.COLOR_BGR2GRAY)
23-
print("Converted RGB image to grayscale...")
24-
print("Resizing image to 28x28 scale...")
25-
img_ = cv2.resize(gray,(28,28))
26-
print("Resized...")
27-
img_resized = cv2.imwrite(filename='saved_img-final.jpg', img=img_)
28-
print("Image saved!")
29-
30-
break
31-
elif key == ord('q'):
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+
cv2.imwrite(filename='saved_img.jpg', img=frame)
14+
webcam.release()
15+
img_new = cv2.imread('saved_img.jpg', cv2.IMREAD_GRAYSCALE)
16+
img_new = cv2.imshow("Captured Image", img_new)
17+
cv2.waitKey(1650)
18+
cv2.destroyAllWindows()
19+
print("Processing image...")
20+
img_ = cv2.imread('saved_img.jpg', cv2.IMREAD_ANYCOLOR)
21+
print("Converting RGB image to grayscale...")
22+
gray = cv2.cvtColor(img_, cv2.COLOR_BGR2GRAY)
23+
print("Converted RGB image to grayscale...")
24+
print("Resizing image to 28x28 scale...")
25+
img_ = cv2.resize(gray,(28,28))
26+
print("Resized...")
27+
img_resized = cv2.imwrite(filename='saved_img-final.jpg', img=img_)
28+
print("Image saved!")
29+
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+
39+
except(KeyboardInterrupt):
3240
print("Turning off camera.")
3341
webcam.release()
3442
print("Camera off.")
3543
print("Program ended.")
3644
cv2.destroyAllWindows()
3745
break
46+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /