def clear_folds(): try: current_time_y = datetime.datetime.now().strftime('%Y') current_time_m = datetime.datetime.now().strftime('%m') current_time_d = datetime.datetime.now().strftime('%d') scheduler = BackgroundScheduler() # 定时清理 scheduler.add_job(func=clear_local_business_logs, args=(current_time_y, current_time_m, current_time_d), trigger='cron', month='*', day='*', hour='0', minute='0') scheduler.add_job(func=clear_local_capture_images, args=(current_time_y, current_time_m, current_time_d), trigger='cron', month='*', day='*', hour='0', minute='0') scheduler.start() except BaseException as e: log_helper.log_out( 'error', 'File: ' + e.__traceback__.tb_frame.f_globals['__file__'] + ', lineon: ' + str(e.__traceback__.tb_lineno) + ', error info: ' + str(e)) raise
def socket_conn(self): try: # connet servier self.tcp_client_socket.connect((self.server_ip, self.server_port)) self.conn_status = 1 except BaseException as e: log_helper.log_out( 'error', 'File: ' + e.__traceback__.tb_frame.f_globals['__file__'] + ', lineon: ' + str(e.__traceback__.tb_lineno) + ', error info: ' + str(e)) self.tcp_client_socket.close() self.conn_status = 0
def detection_persistence(detect_time, images, camera_url): try: path = local_cap_video_path + '/' + camera_url + '/' + detect_time.split( )[0] if not os.path.exists(path): os.makedirs(path) cv2.imwrite(path + '/' + detect_time + '.jpg', images) except BaseException as e: log_helper.log_out( 'error', 'File: ' + e.__traceback__.tb_frame.f_globals['__file__'] + ', lineon: ' + str(e.__traceback__.tb_lineno) + ', error info: ' + str(e)) raise
def socket_cend(self, target_info, detect_img, detect_time, camera_number, disperse_sign): try: if disperse_sign == '1' and len(target_info) != 0: self.send_data = { "disperseSign": disperse_sign, "detectTargetInfo": target_info, "detectTime": detect_time, "cameraNumber": camera_number } self.tcp_client_socket.send( (str(self.send_data) + '##').encode()) except BaseException as e: log_helper.log_out( 'error', 'File: ' + e.__traceback__.tb_frame.f_globals['__file__'] + ', lineon: ' + str(e.__traceback__.tb_lineno) + ', error info: ' + str(e))
def clear_local_business_logs(current_time_y, current_time_m, current_time_d): try: data_folder_path = os.listdir(local_business_logs_path) for date_folder in data_folder_path: if (datetime.date(int(current_time_y), int(current_time_m), int(current_time_d)) - datetime.date(int(date_folder.split('-')[0]), int(date_folder.split('-')[1]), int(date_folder.split('-')[2][:5])) ).days > ftp_images_retain_time: shutil.rmtree( os.path.join(local_business_logs_path, date_folder)) log_helper.log_out('info', str(time.time()) + ' 日志清理完成') except BaseException as e: log_helper.log_out( 'error', 'File: ' + e.__traceback__.tb_frame.f_globals['__file__'] + ', lineon: ' + str(e.__traceback__.tb_lineno) + ', error info: ' + str(e)) raise
def clear_local_capture_images(current_time_y, current_time_m, current_time_d): try: ip_layer = os.listdir(local_cap_video_path) for ip_folder in ip_layer: ip_folder_path = os.path.join(local_cap_video_path, ip_folder) if os.path.isdir(ip_folder_path): date_layer = os.listdir(ip_folder_path) for date_folder in date_layer: if (datetime.date(int(current_time_y), int(current_time_m), int(current_time_d)) - datetime.date(int(date_folder.split('-')[0]), int(date_folder.split('-')[1]), int(date_folder.split('-')[2])) ).days > ftp_images_retain_time: shutil.rmtree(os.path.join(ip_folder_path, date_folder)) log_helper.log_out('info', str(time.time()) + ' 检测目标图像清理完成') except BaseException as e: log_helper.log_out( 'error', 'File: ' + e.__traceback__.tb_frame.f_globals['__file__'] + ', lineon: ' + str(e.__traceback__.tb_lineno) + ', error info: ' + str(e)) raise
def image_put(queue, queue1, camera_url): # continuous_interruption_count = 0 log_helper.log_out('info', str(time.time()) + ' ' + camera_url + ' 加载影像数据') try: release_camera_tiem = time.time() capture = cv2.VideoCapture(camera_url) while True: status, frame = capture.read() if not status: # continuous_interruption_count += 1 log_helper.log_out('info', camera_url + ' 视频流发现问题,矫正中...') capture = cv2.VideoCapture(camera_url) status, frame = capture.read() # if not status: # continuous_interruption_count += 1 # else: # continuous_interruption_count = 0 # 连续中断指定帧数则退出 # if continuous_interruption_count >= 10: # raise IOError(str(camera_url) + ' 摄像头发生异常而中断!') if time.time() - release_camera_tiem > 30: print(camera_url + ' 释放摄像头中。。。') release_camera(capture) release_camera_tiem = time.time() capture = cv2.VideoCapture(camera_url) status, frame = capture.read() queue.put(frame) if queue.qsize() > 1: queue.get() else: time.sleep(0.04) if queue1.qsize() != 0: return print('加载数据模块退出') # capture.release() except BaseException as e: log_helper.log_out( 'error', 'File: ' + e.__traceback__.tb_frame.f_globals['__file__'] + ', lineon: ' + str(e.__traceback__.tb_lineno) + ', error info: ' + str(e)) raise
def cam(queue, queue1, camera_url): demonet = 'vgg16' tfmodel = project_address + '/default/voc_2007_trainval/default_bird/vgg16_faster_rcnn_iter_300000.ckpt' if not os.path.isfile(tfmodel + '.meta'): log_helper.log_out('info', tfmodel) raise IOError( ('{:s} not found.\nDid you download the proper networks from ' 'our server and place them properly?').format(tfmodel + '.meta')) # set config tfconfig = tf.ConfigProto(allow_soft_placement=True) tfconfig.gpu_options.allow_growth = True # init session sess = tf.Session(config=tfconfig) # load networkzhe if demonet == 'vgg16': net = vgg16(batch_size=1) else: raise NotImplementedError try: n_classes = len(CLASSES) # create the structure of the net having a certain shape (which depends on the number of classes) net.create_architecture(sess, "TEST", n_classes, tag='default', anchor_scales=[8, 16, 32, 64]) saver = tf.train.Saver() saver.restore(sess, tfmodel) print('Loaded network {:s}'.format(tfmodel)) log_helper.log_out('info', 'Loaded network {:s}'.format(tfmodel)) log_helper.log_out('info', str(time.time()) + ' Monitoring ...') # 发现目标的连续次数 lazy_frequency = 0 # 记录上次触发驱鸟跑时间 dispersed_time = 0. # 休眠一会以保证数据先加入缓冲区 # time.sleep(2) while True: frame = queue.get() lazy_frequency, dispersed_time = demo_video( sess, net, frame, camera_url, lazy_frequency, dispersed_time) # cv2.imshow('',frame) # time.sleep(0.01) key = cv2.waitKey(1) if key == ord('q') or key == ord( 'Q') or key == 27: # ESC:27 key: quit program break print(str(time.time()) + ' ' + camera_url + ' 识别系统已关闭') log_helper.log_out('info', str(time.time()) + ' ' + camera_url + ' 识别系统已关闭') return except BaseException as e: log_helper.log_out( 'error', camera_url + 'File: ' + e.__traceback__.tb_frame.f_globals['__file__'] + ', lineon: ' + str(e.__traceback__.tb_lineno) + ', error info: ' + str(e)) raise finally: cv2.destroyAllWindows() sess.close() queue1.put(0)
def demo_video(sess, net, frame, camera_url, lazy_frequency, dispersed_time): """Detect object classes in an image usi, ng pre-computed object proposals.""" im = frame timer = Timer() timer.tic() scores, boxes = im_detect(sess, net, im) timer.toc() images = frame # socket 和图片存储路径以ip命名 camera_url_fold_name = camera_url.split('@')[1].split(':')[0] # 在一针图像中是否找到目标 find_target_flag = False # 一帧频图像中每一类目标个数 target_info = dict() detect_time = 0. # Visualize detections for each class CONF_THRESH = detect_threshold # threshold NMS_THRESH = nms_threshold sc = socket_c() try: video_name = '' for cls_ind, cls in enumerate(CLASSES[1:]): cls_ind += 1 # because we skipped background cls_boxes = boxes[:, 4 * cls_ind:4 * (cls_ind + 1)] cls_scores = scores[:, cls_ind] dets = np.hstack( (cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) keep = nms(dets, NMS_THRESH) dets = dets[keep, :] inds = np.where(dets[:, -1] >= CONF_THRESH)[0] images, invalid_target_ele, valid_target_info = vis_detections_video( im, cls, dets, timer.start_time, timer.total_time, inds, CONF_THRESH, camera_url) frame = cv2.resize(images, (int(1920 // 2), int(1080 // 2)), interpolation=cv2.INTER_CUBIC) video_name = camera_name_list[camera_url_list.index(camera_url)] video_name = video_name.split('##') coordinate = video_name[1].split('-') cv2.imshow(video_name[0], frame) cv2.moveWindow(video_name[0], int(coordinate[0]), int(coordinate[1])) detect_time = datetime.datetime.now().strftime( '%Y-%m-%d %H:%M:%S.%f') if len(inds) != 0 and cls in TARGET_CLASSES and ( len(inds) - len(invalid_target_ele)) != 0: find_target_flag = True # target_info[cls] = len(inds) - len(invalid_target_ele) tran_cls = seasonal_target_exclusion(detect_time, cls) target_info[tran_cls] = valid_target_info if find_target_flag: lazy_frequency += 1 else: lazy_frequency = 0 if (lazy_frequency == max_lazy_frequency) and ( (time.time() - dispersed_time) > dispersed_rest_time): dispersed_time = time.time() # 多线程检测目标写入磁盘 t_persistence = threading.Thread( target=detection_persistence, daemon=True, args=(detect_time, images, camera_url_fold_name)).start() # t_persistence.join() # 设置主线程等待子线程结束 sc.socket_conn() if sc.get_conn_status() == 1: sc.socket_cend(target_info, '', detect_time, camera_url_fold_name, '1') lazy_frequency = 0 return lazy_frequency, dispersed_time except BaseException as e: log_helper.log_out( 'error', 'File: ' + e.__traceback__.tb_frame.f_globals['__file__'] + ', lineon: ' + str(e.__traceback__.tb_lineno) + ', error info: ' + str(e)) raise finally: sc.socket_clse()
for process in processes: process.join() ''' for index in range(len(camera_url_list)): locals()['queue_' + str(index)] = queue.Queue(maxsize=2) locals()['queue1_' + str(index)] = queue.Queue(maxsize=1) if index == 0: processes.append(mp.Process(target=fun, args=(queue_0, queue1_0, camera_url_list[index]))) elif index == 1: processes.append(mp.Process(target=fun, args=(queue_1, queue1_1, camera_url_list[index]))) elif index == 2: processes.append(mp.Process(target=fun, args=(queue_2, queue1_2, camera_url_list[index]))) elif index == 3: processes.append(mp.Process(target=fun, args=(queue_3, queue1_3, camera_url_list[index]))) elif index == 4: processes.append(mp.Process(target=fun, args=(queue_4, queue1_4, camera_url_list[index]))) elif index == 5: processes.append(mp.Process(target=fun, args=(queue_5, queue1_5, camera_url_list[index]))) for process in processes: process.daemon = True process.start() for process in processes: process.join() ''' except BaseException as e: log_helper.log_out( 'error', 'File: ' + e.__traceback__.tb_frame.f_globals['__file__'] + ', lineon: ' + str(e.__traceback__.tb_lineno) + ', error info: ' + str(e))