1- # Show a windows with the video stream and testing information
1+ # Show a window with the video stream and testing information
22
33# Import required modules
44import configparser
3737if fh != - 1 :
3838 video_capture .set (cv2 .CAP_PROP_FRAME_HEIGHT , fh )
3939
40+ # Read exposure and dark_thresholds from config to use in the main loop
41+ exposure = config .getint ("video" , "exposure" , fallback = - 1 )
42+ dark_threshold = config .getfloat ("video" , "dark_threshold" )
43+
4044# Let the user know what's up
4145print ("""
4246Opening a window with a test feed
@@ -106,7 +110,17 @@ def print_text(line_number, text):
106110
107111 # Grab a single frame of video
108112 ret , frame = video_capture .read ()
109- frame = cv2 .cvtColor (frame , cv2 .COLOR_BGR2GRAY )
113+
114+ try :
115+ # Convert from color to grayscale
116+ # First processing of frame, so frame errors show up here
117+ frame = cv2 .cvtColor (frame , cv2 .COLOR_BGR2GRAY )
118+ except RuntimeError :
119+ pass
120+ except cv2 .error :
121+ print ("\n Unknown camera, please check your 'device_path' config value.\n " )
122+ raise
123+
110124 frame = clahe .apply (frame )
111125 # Make a frame to put overlays in
112126 overlay = frame .copy ()
@@ -133,9 +147,6 @@ def print_text(line_number, text):
133147 # Draw the bar in green
134148 cv2 .rectangle (overlay , p1 , p2 , (0 , 200 , 0 ), thickness = cv2 .FILLED )
135149
136- # Draw a stripe indicating the dark threshold
137- cv2 .rectangle (overlay , (8 , 35 ), (20 , 36 ), (255 , 0 , 0 ), thickness = cv2 .FILLED )
138-
139150 # Print the statis in the bottom left
140151 print_text (0 , "RESOLUTION: %dx%d" % (height , width ))
141152 print_text (1 , "FPS: %d" % (fps , ))
@@ -147,7 +158,7 @@ def print_text(line_number, text):
147158 cv2 .putText (overlay , "SLOW MODE" , (width - 66 , height - 10 ), cv2 .FONT_HERSHEY_SIMPLEX , .3 , (0 , 0 , 255 ), 0 , cv2 .LINE_AA )
148159
149160 # Ignore dark frames
150- if hist_perc [0 ] > 50 :
161+ if hist_perc [0 ] > dark_threshold :
151162 # Show that this is an ignored frame in the top right
152163 cv2 .putText (overlay , "DARK FRAME" , (width - 68 , 16 ), cv2 .FONT_HERSHEY_SIMPLEX , .3 , (0 , 0 , 255 ), 0 , cv2 .LINE_AA )
153164 else :
@@ -156,7 +167,8 @@ def print_text(line_number, text):
156167
157168 rec_tm = time .time ()
158169 # Get the locations of all faces and their locations
159- face_locations = face_detector (frame , 1 ) # upsample 1 time
170+ # Upsample it once
171+ face_locations = face_detector (frame , 1 )
160172 rec_tm = time .time () - rec_tm
161173
162174 # Loop though all faces and paint a circle around them
@@ -193,6 +205,15 @@ def print_text(line_number, text):
193205 if slow_mode :
194206 time .sleep (.5 - frame_time )
195207
208+ if exposure != - 1 :
209+ # For a strange reason on some cameras (e.g. Lenoxo X1E)
210+ # setting manual exposure works only after a couple frames
211+ # are captured and even after a delay it does not
212+ # always work. Setting exposure at every frame is
213+ # reliable though.
214+ video_capture .set (cv2 .CAP_PROP_AUTO_EXPOSURE , 1.0 ) # 1 = Manual
215+ video_capture .set (cv2 .CAP_PROP_EXPOSURE , float (exposure ))
216+
196217# On ctrl+C
197218except KeyboardInterrupt :
198219 # Let the user know we're stopping
0 commit comments