Skip to content

Free Motion Capture for Everyone πŸ’€βœ¨

TypeScript

merge main into dev_branch

Endurance Idehene64389d2b40e
diff --cc freemocap/dev_scratchpad/dev_alpha_old/FMC_MultiCamera.py
index f01a37bd,f3a1693e..d1a5067d
--- a/freemocap/dev_scratchpad/dev_alpha_old/FMC_MultiCamera.py
+++ b/freemocap/dev_scratchpad/dev_alpha_old/FMC_MultiCamera.py
@@@ -317,12 -303,10 +317,12 @@@ class FMC_MultiCamera
for cam_num in range(self.num_cams):
this_cam_image_timestamp_tuple = self.cam_frame_queue.get()
this_cam_num = this_cam_image_timestamp_tuple[0]
- this_cam_index = self.cams_to_use_list.webcam_id(this_cam_num)
+ this_cam_index = self.cams_to_use_list.port_number(this_cam_num)
these_images_list[this_cam_index] = this_cam_image_timestamp_tuple[1]
these_timestamps[this_cam_index] = this_cam_image_timestamp_tuple[2]
- this_multi_cam_tuple_as_a_list[this_cam_index] = this_cam_image_timestamp_tuple
+ this_multi_cam_tuple_as_a_list[
+ this_cam_index
+ ] = this_cam_image_timestamp_tuple
this_multi_cam_tuple = tuple(this_multi_cam_tuple_as_a_list)
@@@ -657,43 -541,31 +657,47 @@@
###
-if __name__ == '__main__':
+if __name__ == "__main__":
pathos_mp_helper.freeze_support()
- console = Console() #create rich console to catch and print exceptions
+ console = Console() # create rich console to catch and print exceptions
import socket
+
this_computer_name = socket.gethostname()
- freemocap_data_path=None
- in_rotation_codes_list=None
+ freemocap_data_path = None
+ in_rotation_codes_list = None
+ in_cams_to_use_list = None
- if this_computer_name=='jon-hallway-XPS-8930':
- freemocap_data_path = Path('/home/jon/Dropbox/FreeMoCapProject/FreeMocap_Data')
-
- elif this_computer_name == 'DESKTOP-DCG6K4F':
- freemocap_data_path = Path(r'C:\Users\jonma\Dropbox\FreeMoCapProject\FreeMocap_Data')
- in_rotation_codes_list = ['cv2.ROTATE_90_COUNTERCLOCKWISE', 'cv2.ROTATE_90_COUNTERCLOCKWISE', 'cv2.ROTATE_90_CLOCKWISE', 'cv2.ROTATE_90_CLOCKWISE', 'cv2.ROTATE_90_CLOCKWISE', ]
- in_cams_to_use_list = [0,1,2,3,4]
- elif this_computer_name == 'DESKTOP-V3D343U':
- freemocap_data_path = Path(r'C:\Users\WindowsPC_Hallway\Dropbox\FreeMoCapProject\FreeMocap_Data')
- elif this_computer_name == 'Jons-MacBook-Pro.local':
- freemocap_data_path = Path('/Users/jon/Dropbox/FreeMoCapProject')
+ if this_computer_name == "jon-hallway-XPS-8930":
+ freemocap_data_path = Path("/home/jon/Dropbox/FreeMoCapProject/FreeMocap_Data")
++
++ elif this_computer_name == "DESKTOP-DCG6K4F":
++ freemocap_data_path = Path(
++ r"C:\Users\jonma\Dropbox\FreeMoCapProject\FreeMocap_Data"
++ )
+ in_rotation_codes_list = [
+ "cv2.ROTATE_90_COUNTERCLOCKWISE",
+ "cv2.ROTATE_90_COUNTERCLOCKWISE",
+ "cv2.ROTATE_90_CLOCKWISE",
+ "cv2.ROTATE_90_CLOCKWISE",
+ "cv2.ROTATE_90_CLOCKWISE",
+ ]
- elif this_computer_name == "DESKTOP-DCG6K4F":
- freemocap_data_path = Path(
- r"C:\Users\jonma\Dropbox\FreeMoCapProject\FreeMocap_Data"
- )
++ in_cams_to_use_list = [0, 1, 2, 3, 4]
+ elif this_computer_name == "DESKTOP-V3D343U":
+ freemocap_data_path = Path(
+ r"C:\Users\WindowsPC_Hallway\Dropbox\FreeMoCapProject\FreeMocap_Data"
+ )
+ elif this_computer_name == "Jons-MacBook-Pro.local":
+ freemocap_data_path = Path("/Users/jon/Dropbox/FreeMoCapProject")
try:
- multi_cam = FMC_MultiCamera(freemocap_data_folder=str(freemocap_data_path), save_each_cam_to_mp4 = True, rotation_codes_list=in_rotation_codes_list, cams_to_use_list=in_cams_to_use_list)
+ multi_cam = FMC_MultiCamera(
+ freemocap_data_folder=str(freemocap_data_path),
+ save_each_cam_to_mp4=True,
+ rotation_codes_list=in_rotation_codes_list,
++ cams_to_use_list=in_cams_to_use_list,
+ )
multi_cam.start(standalone_mode=True)
except Exception:
diff --cc freemocap/dev_scratchpad/dev_alpha_old/capture_volume_calibration.py
index ffcdf4e6,34b4603f..7a6666e5
--- a/freemocap/dev_scratchpad/dev_alpha_old/capture_volume_calibration.py
+++ b/freemocap/dev_scratchpad/dev_alpha_old/capture_volume_calibration.py
@@@ -16,9 -16,8 +16,9 @@@ aruco_dict = cv2.aruco.Dictionary_get(c
charuco_length = 7
charuco_width = 5
--board = cv2.aruco.CharucoBoard_create(charuco_length, charuco_width, 1, .8, aruco_dict)
-num_charuco_corners = (charuco_length-1) * (charuco_width-1)
++board = cv2.aruco.CharucoBoard_create(charuco_length, charuco_width, 1, 0.8, aruco_dict)
+num_charuco_corners = (charuco_length - 1) * (charuco_width - 1)
+
def detect_charuco_board(image):
"""
@@@ -32,18 -31,19 +32,21 @@@
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 100, 0.00001)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
-- aruco_square_corners, aruco_square_ids, rejectedImgPoints = cv2.aruco.detectMarkers(gray, aruco_dict)
-
++ aruco_square_corners, aruco_square_ids, rejectedImgPoints = cv2.aruco.detectMarkers(
++ gray, aruco_dict
++ )
- if len(aruco_square_corners)>0:
+ if len(aruco_square_corners) > 0:
# SUB PIXEL DETECTION
for this_corner in aruco_square_corners:
-- cv2.cornerSubPix(gray, this_corner,
- winSize=(3, 3),
- zeroZone=(-1, -1),
- criteria=criteria)
- res2 = cv2.aruco.interpolateCornersCharuco(aruco_square_corners, aruco_square_ids, gray, board)
- winSize = (3,3),
- zeroZone = (-1,-1),
- criteria = criteria)
- res2 = cv2.aruco.interpolateCornersCharuco(aruco_square_corners,aruco_square_ids,gray,board)
-
- if res2[1] is not None and res2[2] is not None and len(res2[1])>3:
++ cv2.cornerSubPix(
++ gray, this_corner, winSize=(3, 3), zeroZone=(-1, -1), criteria=criteria
++ )
++ res2 = cv2.aruco.interpolateCornersCharuco(
++ aruco_square_corners, aruco_square_ids, gray, board
++ )
+
+ if res2[1] is not None and res2[2] is not None and len(res2[1]) > 3:
charuco_corners = res2[1]
charuco_ids = res2[2]
@@@ -57,41 -59,42 +60,55 @@@ if __name__ == "__main__"
this_computer_name = socket.gethostname()
- freemocap_data_path=None
- in_rotation_codes_list=None
+ freemocap_data_path = None
+ in_rotation_codes_list = None
in_cams_to_use_list = None
-
- if this_computer_name=='jon-hallway-XPS-8930':
- freemocap_data_path = Path('/home/jon/Dropbox/FreeMoCapProject/FreeMocap_Data')
- in_rotation_codes_list = ['cv2.ROTATE_90_COUNTERCLOCKWISE', 'cv2.ROTATE_90_COUNTERCLOCKWISE', 'cv2.ROTATE_90_CLOCKWISE', 'cv2.ROTATE_90_CLOCKWISE', 'cv2.ROTATE_90_CLOCKWISE', ]
- elif this_computer_name == 'DESKTOP-DCG6K4F':
- freemocap_data_path = Path(r'C:\Users\jonma\Dropbox\FreeMoCapProject\FreeMocap_Data')
- elif this_computer_name == 'DESKTOP-V3D343U':
- freemocap_data_path = Path(r'C:\Users\WindowsPC_Hallway\Dropbox\FreeMoCapProject\FreeMocap_Data')
- in_cams_to_use_list = [0,1,2,3]
- elif this_computer_name == 'Jons-MacBook-Pro.local':
- freemocap_data_path = Path('/Users/jon/Dropbox/FreeMoCapProject')
+
- if this_computer_name == 'jon-hallway-XPS-8930':
- freemocap_data_path = Path('/home/jon/Dropbox/FreeMoCapProject/FreeMocap_Data')
- in_rotation_codes_list = ['cv2.ROTATE_90_COUNTERCLOCKWISE', 'cv2.ROTATE_90_COUNTERCLOCKWISE',
- 'cv2.ROTATE_90_CLOCKWISE', 'cv2.ROTATE_90_CLOCKWISE', 'cv2.ROTATE_90_CLOCKWISE', ]
- elif this_computer_name == 'DESKTOP-DCG6K4F':
- freemocap_data_path = Path(r'C:\Users\jonma\Dropbox\FreeMoCapProject\FreeMocap_Data')
- elif this_computer_name == 'DESKTOP-V3D343U':
- freemocap_data_path = Path(r'C:\Users\WindowsPC_Hallway\Dropbox\FreeMoCapProject\FreeMocap_Data')
++ if this_computer_name == "jon-hallway-XPS-8930":
++ freemocap_data_path = Path("/home/jon/Dropbox/FreeMoCapProject/FreeMocap_Data")
++ in_rotation_codes_list = [
++ "cv2.ROTATE_90_COUNTERCLOCKWISE",
++ "cv2.ROTATE_90_COUNTERCLOCKWISE",
++ "cv2.ROTATE_90_CLOCKWISE",
++ "cv2.ROTATE_90_CLOCKWISE",
++ "cv2.ROTATE_90_CLOCKWISE",
++ ]
++ elif this_computer_name == "DESKTOP-DCG6K4F":
++ freemocap_data_path = Path(
++ r"C:\Users\jonma\Dropbox\FreeMoCapProject\FreeMocap_Data"
++ )
++ elif this_computer_name == "DESKTOP-V3D343U":
++ freemocap_data_path = Path(
++ r"C:\Users\WindowsPC_Hallway\Dropbox\FreeMoCapProject\FreeMocap_Data"
++ )
+ in_cams_to_use_list = [0, 1, 2, 3]
- elif this_computer_name == 'Jons-MacBook-Pro.local':
- freemocap_data_path = Path('/Users/jon/Dropbox/FreeMoCapProject')
++ elif this_computer_name == "Jons-MacBook-Pro.local":
++ freemocap_data_path = Path("/Users/jon/Dropbox/FreeMoCapProject")
try:
- multi_cam = FMC_MultiCamera(cams_to_use_list=in_cams_to_use_list, show_multi_cam_stream_bool=False,
- freemocap_data_folder=str(freemocap_data_path),
- rotation_codes_list=in_rotation_codes_list)
- multi_cam = FMC_MultiCamera(cams_to_use_list = in_cams_to_use_list, show_multi_cam_stream_bool = False, freemocap_data_folder=str(freemocap_data_path), rotation_codes_list=in_rotation_codes_list)
++ multi_cam = FMC_MultiCamera(
++ cams_to_use_list=in_cams_to_use_list,
++ show_multi_cam_stream_bool=False,
++ freemocap_data_folder=str(freemocap_data_path),
++ rotation_codes_list=in_rotation_codes_list,
++ )
multi_cam.start()
-
-
-
-- console.rule('Launching Multi Cam Viewer')
++ console.rule("Launching Multi Cam Viewer")
current_loop_count = 0
- aruco_square_corners = [ [] for _ in range(multi_cam.num_cams) ]
- aruco_square_ids = [ np.array([]) for _ in range(multi_cam.num_cams) ]
- charuco_corners = [ np.array([]) for _ in range(multi_cam.num_cams) ]
- charuco_ids = [ np.array([]) for _ in range(multi_cam.num_cams) ]
-
-
- camera_pair_list = list(combinations(np.arange(multi_cam.num_cams),2))
- camera_pair_joint_view_count = [ 0 for _ in range(len(camera_pair_list)) ]
- each_cameras_shared_board_view_count_array = np.zeros((multi_cam.num_cams, multi_cam.num_cams))
+ aruco_square_corners = [[] for _ in range(multi_cam.num_cams)]
+ aruco_square_ids = [np.array([]) for _ in range(multi_cam.num_cams)]
+ charuco_corners = [np.array([]) for _ in range(multi_cam.num_cams)]
+ charuco_ids = [np.array([]) for _ in range(multi_cam.num_cams)]
+
+ camera_pair_list = list(combinations(np.arange(multi_cam.num_cams), 2))
+ camera_pair_joint_view_count = [0 for _ in range(len(camera_pair_list))]
- each_cameras_shared_board_view_count_array = np.zeros((multi_cam.num_cams, multi_cam.num_cams))
++ each_cameras_shared_board_view_count_array = np.zeros(
++ (multi_cam.num_cams, multi_cam.num_cams)
++ )
each_cameras_shared_board_view_count_total = np.zeros(multi_cam.num_cams)
- charuco_points_from_previous_frames = [ [] for _ in range(multi_cam.num_cams) ]
+ charuco_points_from_previous_frames = [[] for _ in range(multi_cam.num_cams)]
multi_cam_queue_size_list = []
@@@ -129,86 -134,82 +146,116 @@@
multi_cam.initialize_each_cam_output_video(this_multi_cam_tuple)
for this_cam_num in range(multi_cam.num_cams):
- multi_cam._each_cam_video_writer_object_list[this_cam_num].write(
- this_multi_cam_tuple[this_cam_num][1])
- multi_cam._each_cam_video_writer_object_list[this_cam_num].write(this_multi_cam_tuple[this_cam_num][1])
-
-
++ multi_cam._each_cam_video_writer_object_list[
++ this_cam_num
++ ].write(this_multi_cam_tuple[this_cam_num][1])
new_cam_image_list = []
- full_charuco_detected_on_this_frame = [False for _ in range(multi_cam.num_cams)]
- full_charuco_detected_on_this_frame = [ False for _ in range(multi_cam.num_cams) ]
++ full_charuco_detected_on_this_frame = [
++ False for _ in range(multi_cam.num_cams)
++ ]
for this_cam_num in range(multi_cam.num_cams):
this_cam_tuple = this_multi_cam_tuple[this_cam_num]
this_cam_image = this_cam_tuple[1]
if current_loop_count % 10 == 0:
- charuco_corners[this_cam_num], charuco_ids[this_cam_num], aruco_square_corners[this_cam_num], \
- aruco_square_ids[this_cam_num] = detect_charuco_board(this_cam_image)
- charuco_corners[this_cam_num], charuco_ids[this_cam_num], aruco_square_corners[this_cam_num], aruco_square_ids[this_cam_num] = detect_charuco_board(this_cam_image)
-
- if len(charuco_ids[this_cam_num]) == num_charuco_corners:
++ (
++ charuco_corners[this_cam_num],
++ charuco_ids[this_cam_num],
++ aruco_square_corners[this_cam_num],
++ aruco_square_ids[this_cam_num],
++ ) = detect_charuco_board(this_cam_image)
+
+ if len(charuco_ids[this_cam_num]) == num_charuco_corners:
full_charuco_detected_on_this_frame[this_cam_num] = True
-
# image_w_markers = cv2.aruco.drawDetectedMarkers(this_cam_image, aruco_square_corners[this_cam_num], aruco_square_ids[this_cam_num])
- image_w_markers = cv2.aruco.drawDetectedCornersCharuco(this_cam_image,
- np.array(charuco_corners[this_cam_num]),
- np.array(charuco_ids[this_cam_num]))
- image_w_markers = cv2.aruco.drawDetectedCornersCharuco(this_cam_image, np.array(charuco_corners[this_cam_num]), np.array(charuco_ids[this_cam_num]))
++ image_w_markers = cv2.aruco.drawDetectedCornersCharuco(
++ this_cam_image,
++ np.array(charuco_corners[this_cam_num]),
++ np.array(charuco_ids[this_cam_num]),
++ )
this_cam_name = "Camera " + str(this_cam_num)
- text_to_write_on_this_camera = ''
- current_cam_corner_count_str = this_cam_name + ": " + str(
- len(charuco_ids[this_cam_num])) + " of " + str(
- num_charuco_corners) + " detected | Full Board: " + str(
- full_charuco_detected_on_this_frame[this_cam_num])
- text_to_write_on_this_camera =''
- current_cam_corner_count_str = this_cam_name + ": " + str(len(charuco_ids[this_cam_num])) + " of " + str(num_charuco_corners) + " detected | Full Board: " + str(full_charuco_detected_on_this_frame[this_cam_num])
- this_cam_shared_views_str = " | Shared Views: " + str(each_cameras_shared_board_view_count_total[this_cam_num])
- text_to_write_on_this_camera = current_cam_corner_count_str + this_cam_shared_views_str
-
- if this_cam_num==0:
- queue_size_str = "Multi-Camera Queue Size: " + str(multi_cam.multi_cam_tuple_queue.qsize())
- queue_text_position = (10,20)
++ text_to_write_on_this_camera = ""
++ current_cam_corner_count_str = (
++ this_cam_name
++ + ": "
++ + str(len(charuco_ids[this_cam_num]))
++ + " of "
++ + str(num_charuco_corners)
++ + " detected | Full Board: "
++ + str(full_charuco_detected_on_this_frame[this_cam_num])
++ )
+ this_cam_shared_views_str = " | Shared Views: " + str(
- each_cameras_shared_board_view_count_total[this_cam_num])
- text_to_write_on_this_camera = current_cam_corner_count_str + this_cam_shared_views_str
++ each_cameras_shared_board_view_count_total[this_cam_num]
++ )
++ text_to_write_on_this_camera = (
++ current_cam_corner_count_str + this_cam_shared_views_str
++ )
+
+ if this_cam_num == 0:
- queue_size_str = "Multi-Camera Queue Size: " + str(multi_cam.multi_cam_tuple_queue.qsize())
++ queue_size_str = "Multi-Camera Queue Size: " + str(
++ multi_cam.multi_cam_tuple_queue.qsize()
++ )
+ queue_text_position = (10, 20)
cv2.putText(
- image_w_markers, #numpy array on which text is written
- queue_size_str, #text
- queue_text_position, #position at which writing has to start
- cv2.FONT_HERSHEY_SIMPLEX, #font family
- .5, #font size
- (255, 10, 100, 255), #font color
- 2) #font stroke
+ image_w_markers, # numpy array on which text is written
+ queue_size_str, # text
+ queue_text_position, # position at which writing has to start
+ cv2.FONT_HERSHEY_SIMPLEX, # font family
- .5, # font size
++ 0.5, # font size
+ (255, 10, 100, 255), # font color
- 2) # font stroke
++ 2,
++ ) # font stroke
cv2.putText(
- image_w_markers, #numpy array on which text is written
- queue_size_str, #text
- queue_text_position, #position at which writing has to start
- cv2.FONT_HERSHEY_SIMPLEX, #font family
- .5, #font size
- (30, 10, 0, 255), #font color
- 3) #font stroke
-
-
- position = (10,50)
+ image_w_markers, # numpy array on which text is written
+ queue_size_str, # text
+ queue_text_position, # position at which writing has to start
+ cv2.FONT_HERSHEY_SIMPLEX, # font family
- .5, # font size
++ 0.5, # font size
+ (30, 10, 0, 255), # font color
- 3) # font stroke
++ 3,
++ ) # font stroke
+
+ position = (10, 50)
cv2.putText(
- image_w_markers, #numpy array on which text is written
- text_to_write_on_this_camera, #text
- position, #position at which writing has to start
- cv2.FONT_HERSHEY_SIMPLEX, #font family
- .5, #font size
- (30, 10, 0, 255), #font color
- 3) #font stroke
+ image_w_markers, # numpy array on which text is written
+ text_to_write_on_this_camera, # text
+ position, # position at which writing has to start
+ cv2.FONT_HERSHEY_SIMPLEX, # font family
- .5, # font size
++ 0.5, # font size
+ (30, 10, 0, 255), # font color
- 3) # font stroke
++ 3,
++ ) # font stroke
cv2.putText(
- image_w_markers, #numpy array on which text is written
- text_to_write_on_this_camera, #text
- position, #position at which writing has to start
- cv2.FONT_HERSHEY_SIMPLEX, #font family
- .5, #font size
- (209, 180, 0, 255), #font color
- 1) #font stroke
-
+ image_w_markers, # numpy array on which text is written
+ text_to_write_on_this_camera, # text
+ position, # position at which writing has to start
+ cv2.FONT_HERSHEY_SIMPLEX, # font family
- .5, # font size
++ 0.5, # font size
+ (209, 180, 0, 255), # font color
- 1) # font stroke
++ 1,
++ ) # font stroke
if full_charuco_detected_on_this_frame[this_cam_num]:
- cv2.polylines(image_w_markers, np.int32([charuco_corners[this_cam_num]]), True, (0, 100, 255),
- 2)
- cv2.polylines(image_w_markers, np.int32([charuco_corners[this_cam_num]]), True, (0,100,255), 2)
++ cv2.polylines(
++ image_w_markers,
++ np.int32([charuco_corners[this_cam_num]]),
++ True,
++ (0, 100, 255),
++ 2,
++ )
# for these_corners in charuco_points_from_previous_frames[this_cam_num]:
- # if len(these_corners)>0:
- # cv2.polylines(image_w_markers, np.int32([these_corners[this_cam_num]]), True, (0,100,255,255/2), 2)
+ # if len(these_corners)>0:
+ # cv2.polylines(image_w_markers, np.int32([these_corners[this_cam_num]]), True, (0,100,255,255/2), 2)
-- charuco_points_from_previous_frames[this_cam_num].append(charuco_corners[this_cam_num])
++ charuco_points_from_previous_frames[this_cam_num].append(
++ charuco_corners[this_cam_num]
++ )
new_cam_image_list.append(this_cam_image)
@@@ -217,21 -219,23 +264,31 @@@
if full_charuco_detected_on_this_frame[this_cam_num]:
for this_other_camera_num in range(multi_cam.num_cams):
if this_other_camera_num != this_cam_num:
-- if full_charuco_detected_on_this_frame[this_other_camera_num]:
-- each_cameras_shared_board_view_count_array[this_cam_num, this_other_camera_num] += 1
-- each_cameras_shared_board_view_count_total = np.sum(each_cameras_shared_board_view_count_array, axis=1)
++ if full_charuco_detected_on_this_frame[
++ this_other_camera_num
++ ]:
++ each_cameras_shared_board_view_count_array[
++ this_cam_num, this_other_camera_num
++ ] += 1
++ each_cameras_shared_board_view_count_total = np.sum(
++ each_cameras_shared_board_view_count_array, axis=1
++ )
# print(each_cameras_shared_board_view_count)
-
for this_pair_num, this_cam_pair in enumerate(camera_pair_list):
camera_one_id = this_cam_pair[0]
camera_two_id = this_cam_pair[1]
- if full_charuco_detected_on_this_frame[camera_one_id] and full_charuco_detected_on_this_frame[
- camera_two_id]:
- if full_charuco_detected_on_this_frame[camera_one_id] and full_charuco_detected_on_this_frame[camera_two_id]:
++ if (
++ full_charuco_detected_on_this_frame[camera_one_id]
++ and full_charuco_detected_on_this_frame[camera_two_id]
++ ):
camera_pair_joint_view_count[this_pair_num] += 1
-
- #update plots
- multi_cam_queue_size_list.append(multi_cam.multi_cam_tuple_queue.qsize())
-
+ # update plots
- multi_cam_queue_size_list.append(multi_cam.multi_cam_tuple_queue.qsize())
++ multi_cam_queue_size_list.append(
++ multi_cam.multi_cam_tuple_queue.qsize()
++ )
# artist_for_queue_trace[0].set_data((np.arange(current_loop_count),multi_cam_queue_size_list))
# plt.pause(0.01)
@@@ -248,7 -254,7 +305,9 @@@
if key == 27: # exit on ESC
break
- if cv2.getWindowProperty(multi_cam._rec_name, cv2.WND_PROP_VISIBLE) < 1: # break loop if window closed
- if cv2.getWindowProperty(multi_cam._rec_name, cv2.WND_PROP_VISIBLE) < 1: #break loop if window closed
++ if (
++ cv2.getWindowProperty(multi_cam._rec_name, cv2.WND_PROP_VISIBLE) < 1
++ ): # break loop if window closed
break
cv2.destroyAllWindows()
@@@ -260,9 -266,9 +319,8 @@@
for this_cam_num in range(multi_cam.num_cams):
multi_cam._each_cam_video_writer_object_list[this_cam_num].release()
-- console.rule('Shutting down MultiCamera Viewer')
- multi_cam.exit_event.set() #send the 'Exit' signal to everyone.
-
++ console.rule("Shutting down MultiCamera Viewer")
+ multi_cam.exit_event.set() # send the 'Exit' signal to everyone.
-
except Exception:
console.print_exception()
diff --cc jon_scratch/opencv_camera.py
index 504b66b5,8f3aa319..34b344a1
--- a/jon_scratch/opencv_camera.py
+++ b/jon_scratch/opencv_camera.py
@@@ -1,113 -1,79 +1,93 @@@
import logging
- import math
import platform
- import threading
import time
- import cv2
import numpy as np
- from pydantic import BaseModel, PrivateAttr
- from collections import deque
+ import cv2
+ from pydantic import BaseModel
logger = logging.getLogger(__name__)
+ logger.level = logging.INFO
- # OpenCV Implementation of interacting with a camera
- class OpenCVCamera(BaseModel):
- port_number: int = 0
- name: str = f"Camera {port_number}"
- exposure: int = -6
- resolution_width: int = 800
- resolution_height: int = 600
- opencv_video_capture_object: cv2.VideoCapture = None
+ class NoCameraAvailableException(Exception):
+ pass
+
+
- _is_capturing_frames: bool = PrivateAttr(False)
- _running_thread = PrivateAttr(None)
- _last_100_frames: deque = PrivateAttr(deque(maxlen=100))
- _fps: float = PrivateAttr(0)
- _num_frames_processed: int = PrivateAttr(0)
- _frame: tuple = PrivateAttr(None)
- _elapsed: float = PrivateAttr(0)
+ class FailedFrameGrabException(Exception):
+ pass
++
+ class TweakedModel(BaseModel):
class Config:
arbitrary_types_allowed = True
- @property
- def current_fps(self):
- if self._elapsed <= 0:
- return 0
- if self._num_frames_processed <= 0:
- return 0
- return int(math.ceil(self._num_frames_processed / self._elapsed))
- def connect(self):
+ # OpenCV Implementation of interacting with a camera
+ class OpenCVCamera(TweakedModel):
- port_number: int=0
- name: str = 'Camera0' # `camera{}`.format(port_number)
++ port_number: int = 0
++ name: str = "Camera0" # `camera{}`.format(port_number)
+ exposure: int = -8
+ resolution_width: int = 1280
+ resolution_height: int = 720
+ framerate: int = 30
+ opencv_video_capture_object: cv2.VideoCapture = None
+ opencv_video_writer_object: cv2.VideoWriter = None
-
-
- def initialize_video_writer(self, output_file_path: str, ):
++
++ def initialize_video_writer(
++ self,
++ output_file_path: str,
++ ):
+ self.opencv_video_writer_object = cv2.VideoWriter(
+ output_file_path,
- cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'),
- self.framerate,
- (self.resolution_width, self.resolution_height))
-
- logger.info('Initialized video writer for {}'.format(self.name))
-
- def connect(self)->bool:
- if platform.system() == 'Windows':
++ cv2.VideoWriter_fourcc("M", "J", "P", "G"),
++ self.framerate,
++ (self.resolution_width, self.resolution_height),
++ )
++
++ logger.info("Initialized video writer for {}".format(self.name))
++
++ def connect(self) -> bool:
+ if platform.system() == "Windows":
cap_backend = cv2.CAP_DSHOW
else:
cap_backend = cv2.CAP_ANY
-
- self.opencv_video_capture_object = cv2.VideoCapture(self.port_number, cap_backend)
+ self.opencv_video_capture_object = cv2.VideoCapture(
+ self.port_number, cap_backend
+ )
success, image = self.opencv_video_capture_object.read()
- if not success:
- logger.error(
- "Could not connect to a camera at port# {}".format(self.port_number)
- )
- return success
-
- # set camera stream parameters
- # self.opencv_video_capture_object.set(cv2.CAP_PROP_EXPOSURE, self.exposure)
- # self.opencv_video_capture_object.set(
- # cv2.CAP_PROP_FRAME_WIDTH, self.resolution_width
- # )
- # self.opencv_video_capture_object.set(
- # cv2.CAP_PROP_FRAME_HEIGHT, self.resolution_height
- # )
-
- #set camera stream paramters
++ # set camera stream paramters
+ self.opencv_video_capture_object.set(cv2.CAP_PROP_EXPOSURE, self.exposure)
- self.opencv_video_capture_object.set(cv2.CAP_PROP_FRAME_WIDTH, self.resolution_width)
- self.opencv_video_capture_object.set(cv2.CAP_PROP_FRAME_HEIGHT, self.resolution_height)
- self.opencv_video_capture_object.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'))
-
++ self.opencv_video_capture_object.set(
++ cv2.CAP_PROP_FRAME_WIDTH, self.resolution_width
++ )
++ self.opencv_video_capture_object.set(
++ cv2.CAP_PROP_FRAME_HEIGHT, self.resolution_height
++ )
+ self.opencv_video_capture_object.set(
+ cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc("M", "J", "P", "G")
+ )
+
if success:
- logger.debug(f"Camera found at port number {self.port_number}")
- self.name = f"Camera {self.port_number}"
- fps_input_stream = int(self.opencv_video_capture_object.get(5))
- print("FPS of webcam hardware/input stream: {}".format(fps_input_stream))
- logger.error('Camera found at port number {}'.format(self.port_number))
- self.name = 'Camera{}'.format(self.port_number)
++ logger.error("Camera found at port number {}".format(self.port_number))
++ self.name = "Camera{}".format(self.port_number)
+ return success
+ else:
+ # raise NoCameraAvailableException()
- logger.error('Could not connect to a camera at port# {}'.format(self.port_number))
++ logger.error(
++ "Could not connect to a camera at port# {}".format(self.port_number)
++ )
return success
-
- def start_frame_capture(self):
- t = threading.Thread(target=self._start_frame_loop, daemon=True)
- t.start()
- self._running_thread = t
-
- def stop_frame_capture(self):
- self._is_capturing_frames = False
-
- def latest_frame(self):
- if not self._frame:
- return False, None, None
- return self._frame
-
- def _start_frame_loop(self):
- self._is_capturing_frames = True
- start = time.time()
- while self._is_capturing_frames:
- success, image, timestamp = self.get_next_frame()
- self._frame = (success, image, timestamp)
- self._last_100_frames.append((success, image, timestamp))
- self._num_frames_processed += 1
- self._elapsed = time.time() - start
-
+
def get_next_frame(self):
+
timestamp_ns_pre_grab = time.time_ns()
- # Why grab not read? see ->
- # https://stackoverflow.com/questions/57716962/difference-between-video-capture-read-and
- # -grab
+ # Why grab not read? see -> https://stackoverflow.com/questions/57716962/difference-between-video-capture-read-and-grab
grab_success = self.opencv_video_capture_object.grab()
timestamp_ns_post_grab = time.time_ns()
- timestamp_ns = (timestamp_ns_pre_grab + timestamp_ns_post_grab)/2
+ timestamp_ns = (timestamp_ns_pre_grab + timestamp_ns_post_grab) / 2
if grab_success:
success, image = self.opencv_video_capture_object.retrieve()
@@@ -119,4 -84,36 +98,40 @@@
def close(self):
self.opencv_video_capture_object.release()
- logger.info("Closed camera {}".format(self.name))
- logger.info('Closed camera{}'.format(self.name))
++ logger.info("Closed camera{}".format(self.name))
+
+
-if __name__ == '__main__':
++if __name__ == "__main__":
+ from rich.console import Console
++
+ console = Console()
+ timestamps = []
+ try:
+ # Test the camera
- camera = OpenCVCamera()
++ camera = OpenCVCamera()
+ camera.connect()
+
+ while True:
+ success, image, timestamp_ns = camera.get_next_frame()
- timestamps.append(timestamp_ns/1e9)
++ timestamps.append(timestamp_ns / 1e9)
+ if success:
- mean_fps = 1/np.mean(np.diff(timestamps))
- console.print('{} grabbed a frame at timestamp {} : mean fps = {}'.format(camera.name, timestamp_ns/1e9, mean_fps))
++ mean_fps = 1 / np.mean(np.diff(timestamps))
++ console.print(
++ "{} grabbed a frame at timestamp {} : mean fps = {}".format(
++ camera.name, timestamp_ns / 1e9, mean_fps
++ )
++ )
+ else:
- console.print('{} failed to grab a frame at timestamp {} : mean fps = {}'.format(camera.name, timestamp_ns/1e9, mean_fps))
- cv2.imshow(camera.name+'- Press ESC to close', image)
++ console.print(
++ "{} failed to grab a frame at timestamp {} : mean fps = {}".format(
++ camera.name, timestamp_ns / 1e9, mean_fps
++ )
++ )
++ cv2.imshow(camera.name + "- Press ESC to close", image)
+ exit_key = cv2.waitKey(1)
- if exit_key ==27:
++ if exit_key == 27:
+ break
+ cv2.destroyAllWindows()
+ camera.close()
+ except:
+ console.print_exception()
-
-
-
-
-