def search_static_objects(): global search_ptz_p global search_ptz_t global ptz_set_count print(os.path.basename(__file__), utils.get_function_name()) while flag_pause is not True: #if count_birds_from_panoramic() > 0: #return [], None module_camera.set_ptz(search_ptz_p, search_ptz_t, default_ptz_z) # search_ptz_p, search_ptz_t, zoom ptz_set_count += 1 search_ptz_p += ptz_p_delta if search_ptz_p > ptz_p_max: search_ptz_t += ptz_t_delta search_ptz_p = ptz_p_min if search_ptz_t > ptz_t_max: break time_stamp = time.time() ret, img = module_camera.get_image(ptz_channel, flip=False) if ret: rois = module_panoramic.get_split_bbox(img, dst_rows_min=1000, dst_cols_min=1000) objs = module_panoramic.detect_objs_in_rois(img, rois) if show_flag == 'yes' or show_flag == 'true': module_result.show_image(win_name, img, objs) dx, dy = calc_objs_shift(objs, img, limit=0.8) if dx is not None and dy is not None: return objs, img search_ptz_p, search_ptz_t = ptz_p_min, ptz_t_min # reset ptz state to default print('ptz_set_count = %d' % ptz_set_count) ptz_set_count = 0 return [], None
def count_birds_from_panoramic(): print(os.path.basename(__file__), utils.get_function_name()) birds_num = 0 with module_panoramic.lock: for ch in [1,2]: for obj in module_panoramic.final_objects[ch]: if obj['class_name'] in tracking_class: birds_num += 1 return birds_num
def detect_static_objects(img, channel=-1): print(os.path.basename(__file__), utils.get_function_name()) rois = get_split_bbox(img, dst_rows_min=input_image_rows, dst_cols_min=input_image_cols) objs = detect_objs_in_rois(img, rois) return objs
def proc_panoramic_moving(name, param_delay): global static_images print(os.path.basename(__file__), utils.get_function_name()) for k in [1, 2]: while static_images_old[k] is None: print('waiting for static image ready') time.sleep(param_delay) while True: while flag_pause is True: time.sleep(param_delay) for k in [1, 2]: time_stamp = time.time() with lock: img_static = static_images[k].copy() img_static_old = static_images_old[k].copy() objs_static = static_objects[k].copy() ret, img = module_camera.get_image(k) if ret: objs_moving = detect_moving_objects( [img_static_old, img_static, img], time_stamp=time_stamp, channel=k) objs_moving_confirmed = confirm_moving_objects( img, objs_moving) objs_moving_confirmed = filter_moving_objects( img, objs_moving_confirmed) objs_final = merge_objects(objs_static, objs_moving, objs_moving_confirmed) objs_final = filter_moving_objects(img, objs_final) with lock: moving_images[k] = img.copy() moving_objects[k] = objs_moving_confirmed.copy() final_objects[k] = objs_final.copy() with module_result.lock: module_result.result['panoramic'] = objs_final.copy() if show_flag == 'yes' or show_flag == 'true': win_name = 'module_panoramic channel-%d' % k module_result.show_image(win_name, img, objs_final)
def confirm_moving_objects(img, objs): print(os.path.basename(__file__), utils.get_function_name()) img_rows = img.shape[0] img_cols = img.shape[1] rois = list() for obj in objs: roi = obj['bbox'] within_exist_count = 0 for roi_exist in rois: if roi[0] >= roi_exist[0] and roi[1] >= roi_exist[1] and roi[ 2] <= roi_exist[2] and roi[3] <= roi_exist[3]: within_exist_count += 1 if within_exist_count is 0: center = (roi[0] + roi[2]) / 2, (roi[1] + roi[3]) / 2 cols = roi[2] - roi[0] rows = roi[3] - roi[1] if cols < input_image_cols: cols = input_image_cols if rows < input_image_rows: rows = input_image_rows if cols < rows * input_image_cols / input_image_rows: cols = rows * input_image_cols / input_image_rows if rows < cols * input_image_rows / input_image_cols: rows = cols * input_image_rows / input_image_cols xmin = round(center[0] - cols / 2) ymin = round(center[1] - rows / 2) xmax = round(center[0] + cols / 2) ymax = round(center[1] + rows / 2) if xmin < 0: xmin, xmax = 0, xmax - xmin if ymin < 0: ymin, ymax = 0, ymax - ymin if xmax > img_cols: xmax = img_cols if ymax > img_rows: ymax = img_rows roi = (xmin, ymin, xmax, ymax) rois.append(roi) print('confirm_moving_objects: rois =\n', rois) objs = detect_objs_in_rois(img, rois) return objs
def proc_ptz(*args, **kwargs): while True: while flag_pause: time.sleep(param_delay) print(os.path.basename(__file__), utils.get_function_name()) #panoramic_birds_num = count_birds_from_panoramic() #objs_suspected = get_suspected_objects_from_panoramic() #objs_confirmed = [] objs = [] img = None #if not at preset,the ptz camara detect all area #if arrived the preset,get the preset infor and detect the specific area if preset_flag is not True: while True: objs, img = search_static_objects() if len(objs) <= 0: break else: while True: #get_config_from_preset_ini() need to add #objs, img = search_preset_static_objects() need to add if len(objs) <= 0: break """
def detect_moving_objects(frames, time_stamp=0.0, channel=-1): print(os.path.basename(__file__), utils.get_function_name()) t0 = cv.getTickCount() frame_diff = diff_frame(frames) contours = cv.findContours(frame_diff, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)[-2] t1 = cv.getTickCount() #print('detect_moving_objects time elapsed: %08.3f ms' % (1000.0*(t1-t0)/cv.getTickFrequency())) objs = [] for c in contours: x, y, w, h = cv.boundingRect(c) obj = create_object(class_name='unknown', state='moving', time_stamp=time_stamp, channel=channel, bbox=[x, y, x + w, y + h], score=0.0) objs.append(obj) return objs
def proc_panoramic_static(name, param_delay): global static_images print(os.path.basename(__file__), utils.get_function_name()) while True: while flag_pause is True: time.sleep(param_delay) for channel in [1, 2]: ret, img = module_camera.get_image(channel) if ret: objs = detect_static_objects(img, channel) with lock: static_images_old[channel] = static_images[channel] static_images[channel] = img.copy() static_objects[channel] = objs.copy() #show the static result of panoramic with module_result.lock: module_result.result['panoramic'] = objs.copy if show_flag == 'yes' or show_flag == 'true': win_name = 'module_panoramic channel-%d' % channel module_result.show_image(win_name, img, objs) time.sleep(0.1)
def filter_moving_objects(img, objs): print(os.path.basename(__file__), utils.get_function_name()) img_rows = img.shape[0] img_cols = img.shape[1] rois = list() objs_ret = [] for obj in objs: roi = obj['bbox'] # filter bboxes max_pixels = 160 xmin, ymin, xmax, ymax = tuple(roi) roi_w = xmax - xmin roi_h = ymax - ymin if obj['class_name'] == 'person': objs_ret.append(obj) elif obj[ 'class_name'] == 'bird': #and roi_w <= max_pixels and roi_h <= max_pixels: # and (xmin >= img_cols//3 or ymax <= img_rows*0.6): objs_ret.append(obj) return objs_ret
def get_panoramic_birds(): print(os.path.basename(__file__), utils.get_function_name()) return []
def get_suspected_objects_from_panoramic(): get_small_moving_objects() print(os.path.basename(__file__), utils.get_function_name()) return []
def get_small_moving_objects(): print(os.path.basename(__file__), utils.get_function_name()) return []
def track_moving_birds(objs, img): if objs is None: return if len(objs) <= 0: return print(os.path.basename(__file__), utils.get_function_name()) ptz_ret, ptz_p, ptz_t, ptz_z = module_camera.get_ptz() err_p, err_t = calc_rotate_angle(objs, img, ptz_p, ptz_t, ptz_z) ptz_p = ptz_p + err_p ptz_t = ptz_t + err_t module_camera.set_ptz(ptz_p, ptz_t, default_ptz_z, block=True, err_p_max=1.0, err_t_max=1.0) err_p = [0, 0] err_t = [0, 0] time_stamp = [time.time()] * 2 no_obj_count = 0 while flag_pause is not True and ptz_ret == 0: ret, img = module_camera.get_image(ptz_channel, flip=False) if ret: rows = img.shape[0] cols = img.shape[1] rois = module_panoramic.get_split_bbox(img, dst_rows_min=rows, dst_cols_min=cols) objs = module_panoramic.detect_objs_in_rois(img, rois) if len(objs) == 0: no_obj_count += 1 if no_obj_count > 5: print('no object detect, tracking over.') return else: no_obj_count = 0 err_p[1] = err_p[0] err_t[1] = err_t[0] err_p[0], err_t[0] = calc_rotate_angle(objs, img, ptz_p, ptz_t, ptz_z) time_stamp[1] = time_stamp[0] time_stamp[0] = time.time() time_span = time_stamp[0] - time_stamp[1] if time_span > 1.0: time_span = 1.0 print('time_span: ', time_span) # ptz_p = ptz_p + time_span*(pid_i*err_p[0] + pid_p*(err_p[0]-err_p[1])) # ptz_t = ptz_t + time_span*(pid_i*err_t[0] + pid_p*(err_t[0]-err_t[1])) # module_camera.set_ptz(ptz_p, ptz_t, default_ptz_z, block=False) ptz_p = ptz_p + err_p[0] ptz_t = ptz_t + err_t[0] module_camera.set_ptz(ptz_p, ptz_t, default_ptz_z, block=True, err_p_max=0.2, err_t_max=0.2) time.sleep(0.05) if show_flag == 'yes' or show_flag == 'true': module_result.show_image(win_name, img, objs) if flag_pause: print('module_ptz is paused.') return