This repository has been archived by the owner on Mar 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
camera_feed.py
114 lines (97 loc) · 3.18 KB
/
camera_feed.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
"""
camera_feed.py
adapted from select-image-app-prototype.py
Version: 0.1
By: M.J. Camara
Allows user to take a picture of themselves for the program to reference during login
"""
# import opencv
import cv2
import numpy as np
"""
Wrapper class for the select image app.
"""
class CameraFeed:
"""
Returns a photo captured from the default webcam
"""
def capture(self, window_name, image_width, image_height, message):
try:
# Set webcam to variable
camera = cv2.VideoCapture(0)
# Set up user window
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
cv2.setWindowProperty(window_name, cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
# Image brightness offset
brightness = 0
# Captured photo
captured_photo = None
# Infinite loop until escape is pressed or window closed
while cv2.getWindowProperty(window_name, cv2.WND_PROP_VISIBLE) >= 1:
# Read camera frames from webcam
ret, frame = camera.read()
# Grab dimensions of video capture
video_width = camera.get(cv2.CAP_PROP_FRAME_WIDTH)
video_height = camera.get(cv2.CAP_PROP_FRAME_HEIGHT)
# Calculate x and y starting positions for highlight rectangle
x_pos = (int(video_width) // 2) - (image_width)
y_pos = (int(video_height) // 2) - (image_height)
# Draw frame for live image
frame_copy = frame.copy()
cv2.circle(frame_copy,
(int(video_width) // 2, int(video_height) // 2),
152,
(0, 0, 255),
thickness = 1,
lineType = 8,
shift = 0)
cv2.line(frame_copy,
(int(video_width) // 2, (int(video_height) // 2) + 6),
(int(video_width) // 2, (int(video_height) // 2) + 86),
(0, 0, 255),
thickness = 1,
lineType = 8,
shift = 0)
cv2.putText(frame_copy,
message,
(0,25),
cv2.FONT_HERSHEY_TRIPLEX,
1,
(0, 0, 255),
3)
# Show image in rendered window
cv2.imshow(window_name, frame_copy)
# If no frame is retrieved end loop
if not ret:
break
# Set program to wait infinitely until keyboard input occurs
keypress = cv2.waitKey(1)
if keypress % 256 == 27:
# ESC pressed, breaks out of loop
break
elif keypress % 256 == 32:
# SPACE pressed
# Crop image to desired length and return it
captured_photo = frame[y_pos:y_pos + (image_height*2), x_pos:x_pos + (image_width*2)].copy()
captured_photo = cv2.cvtColor(captured_photo, cv2.COLOR_BGR2GRAY)
captured_photo = cv2.resize(captured_photo, (image_width, image_height))
break
elif keypress % 256 == 91:
brightness -= 10
elif keypress % 256 == 93:
brightness += 10
camera.release()
cv2.destroyAllWindows()
# return self.brightness(captured_photo, brightness)
return captured_photo
except cv2.error as e:
print('Default webcam not found. Make sure the device is connected.')
return None
def brighten(self, image, offset):
return image
if __name__ == '__main__':
# Initialize class
add_user_app = CameraFeed()
photo = add_user_app.capture('Add as user', 178, 178, "Test")
if (photo is not None):
cv2.imwrite("test_photograph_3.jpg", photo)