def video_extract_features(vid_path, frame_number, end_frame): # FASTER video reading (decode in separate thread) fvs = FileVideoStream(vid_path, start_frame=frame_number).start() time.sleep(1.0) current_frame = frame_number - 1 frameCount = end_frame - frame_number + 1 roi = [0.25, 0.55, 0.35, 0.65] frame_width = int(fvs.get_width()) frame_height = int(fvs.get_height()) result_width = round(roi[3] * frame_width) - round(roi[2] * frame_width) result_height = round(roi[1] * frame_height) - round(roi[0] * frame_height) buf = np.empty((frameCount, result_height, result_width), np.dtype('uint8')) buf_canny = np.empty((frameCount, result_height, result_width), np.dtype('uint8')) hist_buf = np.empty((frameCount, 16)) print_iter = 0 while fvs.more(): print_iter += 1 # Capture frame-by-frame frame = fvs.read() current_frame += 1 frame_roi = get_ROI(frame, roi) harris_result = get_harris_feature(frame_roi) hist_result = extract_frame_histogram(frame_roi) # canny_result = get_canny_feature(frame_roi) buf[current_frame - frame_number] = harris_result hist_buf[current_frame - frame_number] = hist_result # buf_canny[current_frame - frame_number] = canny_result # cv2.imshow('Frame', harris_result) if divmod(print_iter, 60)[1] == 0: print(f'Progress: {100*(current_frame-frame_number)/(end_frame-frame_number)}%') # Press Q on keyboard to exit if cv2.waitKey(10) & 0xFF == ord('q'): print(f'current_frame: {current_frame}') break if current_frame == end_frame: fvs.stop() break #save numpy matrix of feature frames np.save('library_match_1_segment_harris.npy', buf) np.save('library_match_1_segment_histogram.npy', hist_buf)
def video_extract_harris_features(vid_path, frame_number, end_frame, filename='harris_features'): # FASTER video reading (decode in separate thread) fvs = FileVideoStream(vid_path, start_frame=frame_number).start() time.sleep(1.0) current_frame = frame_number - 1 frameCount = end_frame - frame_number + 1 roi = [0.35, 0.65, 0.35, 0.65] frame_width = int(fvs.get_width()) frame_height = int(fvs.get_height()) result_width = round(roi[3] * frame_width) - round(roi[2] * frame_width) result_height = round(roi[1] * frame_height) - round(roi[0] * frame_height) buf = np.empty((frameCount, result_height, result_width), np.dtype('uint8')) print_iter = 0 while fvs.more(): print_iter += 1 # Capture frame-by-frame frame = fvs.read() current_frame += 1 # extract features from the frame gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) gray = np.float32(gray) gray = gray[round(roi[0] * frame_height):round(roi[1] * frame_height), round(roi[2] * frame_width):round(roi[3] * frame_width)] # frame = frame[round(roi[0]*size_1):round(roi[1]*size_1), round(roi[2]*size_2):round(roi[3]*size_2)] dst = cv2.cornerHarris(gray, 4, 3, 0.04) # result is dilated for marking the corners, not important dst = cv2.dilate(dst, None) # Threshold for an optimal value, it may vary depending on the image. # frame[dst > 0.01 * dst.max()] = [0, 0, 255] harris_result = np.uint8(255 * dst / dst.max()) buf[current_frame - frame_number] = harris_result # cv2.imshow('Frame', harris_result) if divmod(print_iter, 60)[1] == 0: print( f'Progress: {100*(current_frame-frame_number)/(end_frame-frame_number)}%' ) # Press Q on keyboard to exit if cv2.waitKey(10) & 0xFF == ord('q'): print(f'current_frame: {current_frame}') break if current_frame == end_frame: fvs.stop() break #save numpy matrix of feature frames np.save(f'{filename}.npy', buf)