def segmental_log_spectrum_distance(s, shat, winsize, winfunc): size = min(len(s), len(shat)) nf = size / (winsize / 2) - 1 ret = [] for no in xrange(nf): s_i = get_frame(s, winsize, no) shat_i = get_frame(shat, winsize, no) ret.append(log_spectrum_distance(s_i, shat_i, winfunc)) return ret
def initialize_template_scale(self): # Generate random frames to search for a proper template size. random_fnum_list = np.random.randint(low=self.start_fnum, high=self.stop_fnum, size=self.num_init_frames) opt_w_list, bbox_list = list(), list() # Iterate through input video range. During each iteration, fetch the # frame and obtain the optimal calibrated template size. for random_fnum in random_fnum_list: frame = util.get_frame(self.capture, random_fnum, self.gray_flag) bbox, opt_conf, opt_w, _ = self.get_calibrate_results(frame) # Store template info if confidence above an input threshold. if opt_conf > self.conf_thresh: opt_w_list.append(opt_w) bbox_list.append(bbox) # Calculate the median of the optimal widths and rescale accordingly. opt_w, opt_h = self.get_opt_template_dims(opt_w_list) self.pct_img = cv2.resize(self.orig_pct_img, (opt_w, opt_h)) self.pct_mask = cv2.resize(self.orig_pct_mask, (opt_w, opt_h)) # Calculate the region of interest to search for the template. self.template_roi = self.get_opt_template_roi(bbox_list)
def calibrate_test(self): # Iterate through input video range. During each iteration, fetch the # frame and obtain the optimal calibrated template size. start_time = time.time() for fnum in range(self.start_fnum, self.stop_fnum, self.step_size): frame = util.get_frame(self.capture, fnum, self.gray_flag) bbox, opt_conf, opt_w, opt_h = self.get_calibrate_results(frame) # Get the percent sign accuracy according to the default (480, 584) # to (360, 640) rescale change from (24, 32) to (18, 24). orig_conf_list, _ = self.get_tm_results(frame, 1, 0) # Display frame with a confidence label if show_flag is enabled. if self.show_flag: label = "({}, {}) {:0.3f} -> {:0.3f}".format( opt_w, opt_h, orig_conf_list[0], opt_conf) util.show_frame(frame, [bbox], label, self.save_flag, "output/{:07d}.png".format(fnum)) if cv2.waitKey(self.wait_length) & 0xFF == ord('q'): break # Display the time taken to complete the test. frame_count = (self.stop_fnum - self.start_fnum) // self.step_size util.display_fps(start_time, frame_count, "Calibrate")
def standard_test(self): fnum = self.start_fnum time_queue = list() disp_dict = dict() while fnum < self.stop_fnum: start_time = time.time() fnum += self.step_size frame = util.get_frame(self.capture, fnum, gray_flag=True) frame = frame[300:340, 80:220] # 300:340, 200:320 frame = self.param_filter(frame) if self.contour_flag: frame = self.contour_filter(frame) if self.ocr_flag: conf_text = "--psm 7" # Single test line mode. if self.ocr_mode_flag: # Single word mode. conf_text = "--psm 8" text = pytesseract.image_to_string(cv2.bitwise_not(frame), lang="eng", config=conf_text) disp_dict["OCR"] = text util.display_pa_fps(start_time, time_queue, disp_dict) cv2.imshow(self.window_name, frame) if cv2.waitKey(self.step_delay) & 0xFF == ord('q'): break
def get_pct_timeline(self): pct_timeline = list() for fnum in range(self.start_fnum, self.stop_fnum, self.step_size): # Obtain the frame and get the template confidences and locations. frame = util.get_frame(self.capture, fnum, self.gray_flag) confidence_list, _ = self.get_tm_results(frame, 1) # Append to the percent timeline according to if percent was found. if confidence_list: pct_timeline.append(0) else: pct_timeline.append(-1) return pct_timeline
def standard_test(self): fnum = self.start_fnum time_queue = list() disp_dict = dict() while fnum < self.stop_fnum: start_time = time.time() fnum += self.step_size frame = util.get_frame(self.capture, fnum, gray_flag=True) frame = frame[300:340, 80:220] # 300:340, 200:320 if self.tm_flag: self.match_dmg_templates(frame) util.display_pa_fps(start_time, time_queue, disp_dict) cv2.imshow(self.window_name, frame) if cv2.waitKey(self.step_delay) & 0xFF == ord('q'): break
def standard_test(self): for fnum in range(self.start_fnum, self.stop_fnum): frame = util.get_frame(self.capture, fnum) frame = frame[280:, :] frame_HSV = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) mask = cv2.inRange(frame_HSV, (self.low_H, self.low_S, self.low_V), (self.high_H, self.high_S, self.high_V)) res = cv2.bitwise_and(frame, frame, mask=mask) res_inv = cv2.bitwise_and(frame, frame, mask=cv2.bitwise_not(mask)) cv2.imshow(self.window_name, mask) cv2.imshow('Video Capture AND', res) cv2.imshow('Video Capture INV', res_inv) if cv2.waitKey(30) & 0xFF == ord('q'): break
def sweep_test(self): # Iterate through input video range. During each iteration, fetch the # frame and obtain the percent template confidences and bounding boxes. start_time = time.time() for fnum in range(self.start_fnum, self.stop_fnum, self.step_size): frame = util.get_frame(self.capture, fnum, self.gray_flag) confidence_list, bbox_list = self.get_tm_results(frame, 4, 0) # Display and save frame if the respective flags are enabled. if self.show_flag: label_list = ["{:0.3f}".format(i) for i in confidence_list] label = " ".join(label_list) util.show_frame(frame, bbox_list, label, self.save_flag, "output/{:07d}.png".format(fnum)) if cv2.waitKey(self.wait_length) & 0xFF == ord('q'): break # Display the time taken to complete the test. frame_count = (self.stop_fnum - self.start_fnum) // self.step_size util.display_fps(start_time, frame_count, "Sweep")
def record_observation(start_frames, start_displays): # record the user while he uses the displays print("\trecording...") frames = start_frames.tolist() displays = start_displays.tolist() while running: # get the display if paused: displayId = len(config['displays']) - 1 # no display else: display = util.get_display() displayId = config['displays'].index(display) frame = util.get_frame() frames.append(frame.tolist()) displays.append(displayId) print("\tdone") return (np.array(frames), np.array(displays))
def initialize_test(self): # Generate random frames to search for a proper template size. start_time, opt_w_list, bbox_list = time.time(), list(), list() random_fnum_list = np.random.randint(low=self.start_fnum, high=self.stop_fnum, size=self.num_init_frames) # Iterate through input video range. During each iteration, fetch the # frame and obtain the optimal calibrated template size. print("(opt_w, opt_h), (bbox), random_fnum, opt_conf") for random_fnum in random_fnum_list: frame = util.get_frame(self.capture, random_fnum, self.gray_flag) bbox, opt_conf, opt_w, opt_h = self.get_calibrate_results(frame) # Store the template width if above a confidence threshold. if opt_conf > self.conf_thresh: opt_w_list.append(opt_w) bbox_list.append(bbox) print((opt_w, opt_h), bbox, random_fnum, opt_conf) # Display frame with a confidence label if show_flag is enabled. if self.show_flag: orig_conf_list, _ = self.get_tm_results(frame, 1, 0) label = "({}, {}) {:0.3f} -> {:0.3f}".format( opt_w, opt_h, orig_conf_list[0], opt_conf) util.show_frame(frame, [bbox], label, self.save_flag, "output/{:07d}.png".format(random_fnum)) if cv2.waitKey(self.wait_length) & 0xFF == ord('q'): break # Display the optimal dims, ROI, and time taken to complete the test. opt_w, opt_h = self.get_opt_template_dims(opt_w_list) self.template_roi = self.get_opt_template_roi(bbox_list) print("Optimal Template Size: ({}, {})".format(opt_w, opt_h)) print("Optimal ROI bbox: {}".format(self.template_roi)) util.display_fps(start_time, self.num_init_frames, "Initialize") if self.show_flag: util.show_frame(frame, [self.template_roi], wait_flag=True)
def get_precise_match_ranges(self, init_match_ranges): # Iterate through the match ranges, going backwards if at the start # of a match, and going forward if at the end of a match. prec_match_ranges_flat = list() init_match_ranges_flat = init_match_ranges.flatten() for i, fnum_prediction in enumerate(init_match_ranges_flat): fnum = fnum_prediction if i % 2 == 0: current_step_size = -self.prec_step_size else: current_step_size = self.prec_step_size # Iterate through the video using fnum until no percent has been # found for a specified number of frames. while True: frame = util.get_frame(self.capture, fnum, self.gray_flag) confidence_list, _ = self.get_tm_results(frame, 1) # Increment the precise counter if no pct was found. if confidence_list: prec_counter = 0 else: prec_counter += 1 # Exit if there has been no percent found over multiple frames. if prec_counter == self.max_prec_tl_gap_size: prec_match_ranges_flat.append(fnum - current_step_size * (prec_counter + 1)) break elif fnum == 0 or fnum >= self.stop_fnum - self.prec_step_size: prec_match_ranges_flat.append(fnum) break fnum = fnum + current_step_size # Return the match ranges as a list of pairs. return np.reshape(prec_match_ranges_flat, (-1, 2))
def get_port_num_list(self, match_ranges, match_bboxes): start_time = time.time() for i, match_range in enumerate(match_ranges): random_fnum_list = np.random.randint(low=match_range[0], high=match_range[1], size=self.num_port_frames) # print(match_range) x_pos_list = list() for fnum in random_fnum_list: frame = util.get_frame(self.capture, fnum, self.gray_flag) _, bbox_list = self.get_tm_results(frame, 4) for bbox in bbox_list: x_pos_list.append(bbox[0][0]) # print((fnum, conf_list, bbox_list)) port_pos_list = position_tools.get_port_pos_list(x_pos_list) port_num_list = position_tools.get_port_num_list( port_pos_list, match_bboxes[i]) # print(port_num_list) util.display_total_time(start_time, "Port Sweep") return port_num_list
def get_match_ports(self, match_ranges, match_bboxes): # Iterate over all matches, and generate random frame nums to check. match_ports = list() for i, match_range in enumerate(match_ranges): random_fnum_list = np.random.randint(low=match_range[0], high=match_range[1], size=self.num_port_frames) # Iterate over the random frames, and store percent x-positions. x_pos_list = list() for fnum in random_fnum_list: frame = util.get_frame(self.capture, fnum, self.gray_flag) _, bbox_list = self.get_tm_results(frame, 4) for bbox in bbox_list: x_pos_list.append(bbox[0][0]) # Get the ports used for current match and append to total list. port_pos_list = position_tools.get_port_pos_list(x_pos_list) port_num_list = position_tools.get_port_num_list( port_pos_list, match_bboxes[i]) match_ports.append(port_num_list) return match_ports
def main(): if DEBUG: logger = init_logger('example') else: logger = DummyLogger() logger.warning('There will display multiple processbar.') if DEBUG: pbar = tqdm(total=TOTAL, file=sys.stdout) else: pbar = Dummytqdm() n = 0 while n < TOTAL: completed = random.randint(1, 4) time.sleep(0.01) if completed == 4: logger.error('Demonstrate error message:' ' {} job(s) complete.'.format(completed)) update_n = min(TOTAL - n, completed) n += completed time.sleep(0.02) pbar.update(update_n) pbar.close() logger.info('Finished.') logger.info('Another job.') if DEBUG: pbar = tqdm(total=TOTAL, file=sys.stdout) else: pbar = Dummytqdm() for _ in range(TOTAL): time.sleep(0.05) pbar.update() pbar.close() logger.info('Finished.') wrapper(logger, 'Using wrapper', get_frame())
int32_ary_sound = sp.int32(ary_sound) int32_ary_noise = sp.int32(ary_noise) ary2 = sp.int16(int32_ary_sound + int32_ary_noise) data2 = ary2.tostring() synth.writeframes(data2) remain = remain - s sound.close() noise.close() synth.close() infile = 'tools/sound/noisy.wav' signal, params = read_signal(infile, WINSIZE) nf = len(signal) / (WINSIZE / 2) - 1 sig_out = sp.zeros(len(signal), sp.float32) window = sp.hanning(WINSIZE) ms = MinimumStatistics(WINSIZE, window, params[2]) NP_lambda = compute_avgpowerspectrum(signal[0:WINSIZE * int(params[2] / float(WINSIZE) / 3.0)], WINSIZE, window) ms.init_noise_profile(NP_lambda) ss = JointMap(WINSIZE, window) for no in xrange(nf): frame = get_frame(signal, WINSIZE, no) n_pow = ms.compute(frame, no) res = ss.compute_by_noise_pow(frame, n_pow) add_signal(sig_out, res, WINSIZE, no) ms.show_debug_result() write_signal("tools/sound/noise_reduction.wav", params, sig_out)
import cv2 import cvlib as cv import numpy as np from people_classifier.classify_team import classify_person from cvlib.object_detection import draw_bbox from util import get_frame video = cv2.VideoCapture('media/videos/1904-GATC-CONT-vs-PATE.mp4') video.set(cv2.CAP_PROP_POS_FRAMES, get_frame('2:10')) success, frame = video.read() field_sample = cv2.imread('media/field_sample.png') # load the base field image field_hsv = cv2.cvtColor(field_sample, cv2.COLOR_BGR2HSV) # convert the field image into hsv mu, sig = cv2.meanStdDev(field_hsv) # find the mean colour of the base field image devs = 16 # tolerance dark_field_sample = cv2.imread('media/dark_field_sample.png') dark_field_hsv = cv2.cvtColor(dark_field_sample, cv2.COLOR_BGR2HSV) dmu, dsig = cv2.meanStdDev(dark_field_hsv) ddevs = 14 line_sample = cv2.imread('media/line_sample.png') line_hsv = cv2.cvtColor(line_sample, cv2.COLOR_BGR2HSV) lmu, lsig = cv2.meanStdDev(line_hsv) ldevs = 10 if __name__ == '__main__': while video.isOpened(): frame_out = frame.copy()
cv2.imshow('test', img_d) cv2.waitKey(0) # The result displayed is an accumulation of previous contours. mask = np.zeros(img.shape, np.uint8) cv2.drawContours(mask, contours, i, 255, cv2.FILLED) mask = cv2.bitwise_and(img, mask) res = cv2.bitwise_or(res, mask) cv2.imshow('test', res) cv2.waitKey(0) for fnum in [5320, 7020]: # 3400 works fine capture = cv2.VideoCapture("videos/tbh1.mp4") frame = util.get_frame(capture, fnum, gray_flag=True) frame = frame[300:340, 80:220] # 300:340, 200:320 cv2.imshow('frame', frame) cv2.waitKey(0) #frame = cv2.imread('videos/test4.png', cv2.IMREAD_GRAYSCALE) #show_ocr_result(frame) #img2 = cv2.imread('videos/test4.png', cv2.IMREAD_COLOR) #ocr_test(img2, hsv_flag=False) #ocr_test(img2, hsv_flag=False, avg_flag=True) #ocr_test(img2, hsv_flag=False, gau_flag=True) #ocr_test(img2, hsv_flag=False, med_flag=True) #ocr_test(img2, hsv_flag=False, bil_flag=True) #ocr_test(img2, hsv_flag=True)
help="Use the most recent model, named 'recent.model'", action="store_true") parser.add_argument("-d", "--duration", help="number of frames until done") args = parser.parse_args() if args.recent: MODEL_NAME = "recent.model" if args.duration: frame_count = int(args.duration) model = load_model(MODEL_NAME) currDisplayId = 0 for i in range(frame_count): # get some data frame = util.get_frame() x = np.asarray([frame]) # reshape x = x.reshape([-1, 28, 28, 1]) # predict prediction = int(model.predict_classes(x)[0]) if (currDisplayId != prediction): currDisplayId = prediction display = DISPLAY[prediction] if (display != "none"): os.system(f"sway focus output {display}") else: print("ur lookin away")
int32_ary_sound = sp.int32(ary_sound) int32_ary_noise = sp.int32(ary_noise) ary2 = sp.int16(int32_ary_sound + int32_ary_noise) data2 = ary2.tostring() synth.writeframes(data2) remain = remain - s sound.close() noise.close() synth.close() infile = 'tools/sound/noisy.wav' signal, params = read_signal(infile, WINSIZE) nf = len(signal) / (WINSIZE / 2) - 1 sig_out = sp.zeros(len(signal), sp.float32) window = sp.hanning(WINSIZE) ms = MinimumStatistics(WINSIZE, window, params[2]) NP_lambda = compute_avgpowerspectrum( signal[0:WINSIZE * int(params[2] / float(WINSIZE) / 3.0)], WINSIZE, window) ms.init_noise_profile(NP_lambda) ss = JointMap(WINSIZE, window) for no in xrange(nf): frame = get_frame(signal, WINSIZE, no) n_pow = ms.compute(frame, no) res = ss.compute_by_noise_pow(frame, n_pow) add_signal(sig_out, res, WINSIZE, no) ms.show_debug_result() write_signal("tools/sound/noise_reduction.wav", params, sig_out)