视频分析
在rdshare/detection目录下,通过detection_reuse_control.sh脚本调用detection_reuse.py文件
detection_reuse_control.sh中的内容为
#!/bin/bash #调用detection_bash,此版本为复用的版本,即需要存数据库,查数据库操作 for i in $(seq 370000 370001) do python detection_reuse.py --frame_num $i done
首先记录一下detection_reuse.py原有的内容,然后进行修改
#!usr/bin/python # -*- coding: utf-8 -*- import numpy as np import matplotlib matplotlib.use('Agg') import matplotlib.pyplot from matplotlib import pyplot as plt import os import tensorflow as tf from PIL import Image from object_detection.utils import label_map_util from object_detection.utils import visualization_utils as vis_util import datetime # 关闭tensorflow警告 import time import MySQLdb import argparse import sys reload(sys) sys.setdefaultencoding('utf8') os.environ['TF_CPP_MIN_LOG_LEVEL']='3' detection_graph = tf.Graph() # 插入数据,主要针对ssd_inception这一列 def accuracy_test(frame_num, list): print list conn =MySQLdb.connect(user='root',passwd='TJU55b425',host='localhost',port=3306,db='rdshare',charset='utf8') cursor = conn.cursor() sql="INSERT INTO captain_america3_sd (is_detected, frame_num, ssd_inception) VALUES (1,'%s','%s')"%(frame_num, MySQLdb.escape_string(str(list))); cursor.execute(sql) sql="SELECT is_detected FROM captain_america3_sd WHERE frame_num ='%s' "% (frame_num); cursor.execute(sql) cursor.rowcount conn.commit() cursor.close() # 加载模型数据------------------------------------------------------------------------------------------------------- def loading(model_name): with detection_graph.as_default(): od_graph_def = tf.GraphDef() PATH_TO_CKPT = '/home/yanjieliu/models/models/research/object_detection/pretrained_models/'+model_name + '/frozen_inference_graph.pb' with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid: serialized_graph = fid.read() od_graph_def.ParseFromString(serialized_graph) tf.import_graph_def(od_graph_def, name='') return detection_graph # Detection检测------------------------------------------------------------------------------------------------------- def load_image_into_numpy_array(image): (im_width, im_height) = image.size return np.array(image.getdata()).reshape( (im_height, im_width, 3)).astype(np.uint8) # List of the strings that is used to add correct label for each box. PATH_TO_LABELS = os.path.join('/home/yanjieliu/models/models/research/object_detection/data', 'mscoco_label_map.pbtxt') label_map = label_map_util.load_labelmap(PATH_TO_LABELS) categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=90, use_display_name=True) category_index = label_map_util.create_category_index(categories) def Detection(args, frame_num): image_path=args.image_path loading(args.model_name) #start = time.time() with detection_graph.as_default(): with tf.Session(graph=detection_graph) as sess: # for image_path in TEST_IMAGE_PATHS: image = Image.open('%simage-%s.jpeg'%(image_path, frame_num)) # the array based representation of the image will be used later in order to prepare the # result image with boxes and labels on it. image_np = load_image_into_numpy_array(image) # Expand dimensions since the model expects images to have shape: [1, None, None, 3] image_np_expanded = np.expand_dims(image_np, axis=0) image_tensor = detection_graph.get_tensor_by_name('image_tensor:0') # Each box represents a part of the image where a particular object was detected. boxes = detection_graph.get_tensor_by_name('detection_boxes:0') # Each score represent how level of confidence for each of the objects. # Score is shown on the result image, together with the class label. scores = detection_graph.get_tensor_by_name('detection_scores:0') classes = detection_graph.get_tensor_by_name('detection_classes:0') num_detections = detection_graph.get_tensor_by_name('num_detections:0') # Actual detection. (boxes, scores, classes, num_detections) = sess.run( [boxes, scores, classes, num_detections], feed_dict={image_tensor: image_np_expanded}) # Visualization of the results of a detection.将识别结果标记在图片上 vis_util.visualize_boxes_and_labels_on_image_array( image_np, np.squeeze(boxes), np.squeeze(classes).astype(np.int32), np.squeeze(scores), category_index, use_normalized_coordinates=True, line_thickness=8) # output result输出 list = [] for i in range(3): if classes[0][i] in category_index.keys(): class_name = category_index[classes[0][i]]['name'] #detection_to_database(class_name, frame_num) else: class_name = 'N/A' print("object:%s confidence:%s" % (class_name, scores[0][i])) #print(boxes) if(float(scores[0][i])>0.5): list.append(class_name.encode('utf-8')) accuracy_test(frame_num, list) #accuracy_test_frcnn(frame_num, list) # matplotlib输出图片 # Size, in inches, of the output images. IMAGE_SIZE = (20, 12) plt.figure(figsize=IMAGE_SIZE) plt.imshow(image_np) plt.show() plt.close('all') def parse_args(): '''parse args''' parser = argparse.ArgumentParser() parser.add_argument('--image_path', default='/home/yanjieliu/my_opt/data_for_yolo/CAall/') parser.add_argument('--frame_num', default='370272') parser.add_argument('--model_name', default='ssd_inception_v2_coco_2018_01_28') return parser.parse_args() if __name__ == '__main__': # 运行 args=parse_args() start = time.time() #frame_num=int(36000) Detection(args, args.frame_num) end = time.time() print('time:\n') print str(end-start) #将时间写入到文件,方便统计 # with open('./outputs/1to10test_outputs.txt', 'a') as f: # f.write('\n') # f.write(str(end-start))
检测相似度原代码
#!/usr/bin/python # -*- coding: utf-8 -*- import Image import datetime import time import argparse def make_regalur_image(img, size = (256, 256)): return img.resize(size).convert('RGB') def split_image(img, part_size = (64, 64)): w, h = img.size pw, ph = part_size assert w % pw == h % ph == 0 return [img.crop((i, j, i+pw, j+ph)).copy() \ for i in xrange(0, w, pw) \ for j in xrange(0, h, ph)] def hist_similar(lh, rh): assert len(lh) == len(rh) return sum(1 - (0 if l == r else float(abs(l - r))/max(l, r)) for l, r in zip(lh, rh))/len(lh) def calc_similar(li, ri): # return hist_similar(li.histogram(), ri.histogram()) return sum(hist_similar(l.histogram(), r.histogram()) for l, r in zip(split_image(li), split_image(ri))) / 16.0 def calc_similar_by_path(lf, rf): li, ri = make_regalur_image(Image.open(lf)), make_regalur_image(Image.open(rf)) return calc_similar(li, ri) def make_doc_data(lf, rf): li, ri = make_regalur_image(Image.open(lf)), make_regalur_image(Image.open(rf)) li.save(lf + '_regalur.png') ri.save(rf + '_regalur.png') fd = open('stat.csv', 'w') fd.write('\n'.join(l + ',' + r for l, r in zip(map(str, li.histogram()), map(str, ri.histogram())))) # print >>fd, '\n' # fd.write(','.join(map(str, ri.histogram()))) fd.close() import ImageDraw li = li.convert('RGB') draw = ImageDraw.Draw(li) for i in xrange(0, 256, 64): draw.line((0, i, 256, i), fill = '#ff0000') draw.line((i, 0, i, 256), fill = '#ff0000') li.save(lf + '_lines.png') def parse_args(): '''parse args''' parser = argparse.ArgumentParser() parser.add_argument('--image_path', default='/home/yanjieliu/my_opt/data_for_yolo/CAall/') parser.add_argument('--frame_num', default='370001') return parser.parse_args() if __name__ == '__main__': #path = r'test/TEST%d/%d.JPG' args=parse_args() print('%simage-%s.jpeg'%(args.image_path, args.frame_num)) start = time.time() #for i in xrange(1, 2): # print 'test_case_%d: %.3f%%'%(i, \ # calc_similar_by_path('test/TEST%d/%d.JPG'%(i, 1), 'test/TEST%d/%d.JPG'%(i, 2))*100) print 'test_case: %.3f'%( \ calc_similar_by_path('%simage-%d.jpeg'%(args.image_path, int(args.frame_num)-1), '%simage-%s.jpeg'%(args.image_path, args.frame_num))) endtime = time.time() print('time:\n') print str(endtime-start) # make_doc_data('test/TEST4/1.JPG', 'test/TEST4/2.JPG')
通过 (time python histsimilar.py) >& logfile 代码运行脚本并将结果存入logfile文件
第一次修改,将识别结果(物体,识别框)存入数据库
#!usr/bin/python # -*- coding: utf-8 -*- import numpy as np import matplotlib matplotlib.use('Agg') import matplotlib.pyplot from matplotlib import pyplot as plt import os import tensorflow as tf from PIL import Image from object_detection.utils import label_map_util from object_detection.utils import visualization_utils as vis_util import datetime # 关闭tensorflow警告 import time import MySQLdb import argparse import sys reload(sys) sys.setdefaultencoding('utf8') os.environ['TF_CPP_MIN_LOG_LEVEL']='3' detection_graph = tf.Graph() def todatabase(frame_num, list, boxes): conn=MySQLdb.connect(user='aiya',passwd='wWpPtKkp86CjfYit',host='47.93.20.233',port=3306,db='aiya',charset='utf8') cursor = conn.cursor() sql="INSERT INTO ca3_yolo (frame_no, objects, json_yolo) VALUES ('%d','%s','%s')"%(int(frame_num), MySQLdb.escape_string(str(list)),MySQLdb.escape_string(str(boxes))); cursor.execute(sql) #sql="SELECT is_detected FROM captain_america3_sd WHERE frame_num ='%s' "% (frame_num); #cursor.execute(sql) cursor.rowcount conn.commit() cursor.close() # 插入数据,主要针对ssd_inception这一列 def accuracy_test(frame_num, list): print list conn =MySQLdb.connect(user='root',passwd='TJU55b425',host='localhost',port=3306,db='rdshare',charset='utf8') cursor = conn.cursor() sql="INSERT INTO captain_america3_sd (is_detected, frame_num, ssd_inception) VALUES (1,'%s','%s')"%(frame_num, MySQLdb.escape_string(str(list))); cursor.execute(sql) sql="SELECT is_detected FROM captain_america3_sd WHERE frame_num ='%s' "% (frame_num); cursor.execute(sql) cursor.rowcount conn.commit() cursor.close() # 加载模型数据------------------------------------------------------------------------------------------------------- def loading(model_name): with detection_graph.as_default(): od_graph_def = tf.GraphDef() PATH_TO_CKPT = '/home/yanjieliu/models/models/research/object_detection/pretrained_models/'+model_name + '/frozen_inference_graph.pb' with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid: serialized_graph = fid.read() od_graph_def.ParseFromString(serialized_graph) tf.import_graph_def(od_graph_def, name='') return detection_graph # Detection检测------------------------------------------------------------------------------------------------------- def load_image_into_numpy_array(image): (im_width, im_height) = image.size return np.array(image.getdata()).reshape( (im_height, im_width, 3)).astype(np.uint8) # List of the strings that is used to add correct label for each box. PATH_TO_LABELS = os.path.join('/home/yanjieliu/models/models/research/object_detection/data', 'mscoco_label_map.pbtxt') label_map = label_map_util.load_labelmap(PATH_TO_LABELS) categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=90, use_display_name=True) category_index = label_map_util.create_category_index(categories) def Detection(args, frame_num): image_path=args.image_path loading(args.model_name) #start = time.time() with detection_graph.as_default(): with tf.Session(graph=detection_graph) as sess: # for image_path in TEST_IMAGE_PATHS: image = Image.open('%simage-%s.jpeg'%(image_path, frame_num)) # the array based representation of the image will be used later in order to prepare the # result image with boxes and labels on it. image_np = load_image_into_numpy_array(image) # Expand dimensions since the model expects images to have shape: [1, None, None, 3] image_np_expanded = np.expand_dims(image_np, axis=0) image_tensor = detection_graph.get_tensor_by_name('image_tensor:0') # Each box represents a part of the image where a particular object was detected. boxes = detection_graph.get_tensor_by_name('detection_boxes:0') # Each score represent how level of confidence for each of the objects. # Score is shown on the result image, together with the class label. scores = detection_graph.get_tensor_by_name('detection_scores:0') classes = detection_graph.get_tensor_by_name('detection_classes:0') num_detections = detection_graph.get_tensor_by_name('num_detections:0') # Actual detection. (boxes, scores, classes, num_detections) = sess.run( [boxes, scores, classes, num_detections], feed_dict={image_tensor: image_np_expanded}) # Visualization of the results of a detection.将识别结果标记在图片上 vis_util.visualize_boxes_and_labels_on_image_array( image_np, np.squeeze(boxes), np.squeeze(classes).astype(np.int32), np.squeeze(scores), category_index, use_normalized_coordinates=True, line_thickness=8) # output result输出 list = [] for i in range(3): if classes[0][i] in category_index.keys(): class_name = category_index[classes[0][i]]['name'] #detection_to_database(class_name, frame_num) else: class_name = 'N/A' print("object:%s confidence:%s" % (class_name, scores[0][i])) #print(boxes) if(float(scores[0][i])>0.5): list.append(class_name.encode('utf-8')) todatabase(frame_num, list, boxes) #accuracy_test(frame_num, list) #accuracy_test_frcnn(frame_num, list) # matplotlib输出图片 # Size, in inches, of the output images. IMAGE_SIZE = (20, 12) plt.figure(figsize=IMAGE_SIZE) plt.imshow(image_np) plt.show() plt.close('all') def parse_args(): '''parse args''' parser = argparse.ArgumentParser() parser.add_argument('--image_path', default='/home/yanjieliu/my_opt/data_for_yolo/CAall/') parser.add_argument('--frame_num', default='370272') parser.add_argument('--model_name', default='ssd_inception_v2_coco_2018_01_28') return parser.parse_args() if __name__ == '__main__': # 运行 args=parse_args() start = time.time() #frame_num=int(36000) Detection(args, args.frame_num) end = time.time() print('time:\n') print str(end-start) #将时间写入到文件,方便统计 # with open('./outputs/1to10test_outputs.txt', 'a') as f: # f.write('\n') # f.write(str(end-start))
存入的数据为美队3中从370000帧到371000帧
然后修改差异检测代码,更新difference_score值
#!/usr/bin/python # -*- coding: utf-8 -*- import Image import datetime import time import argparse import MySQLdb def ds_to_database(difference_score, frame_num): #将difference_score存入数据库 conn=MySQLdb.connect(user='aiya',passwd='wWpPtKkp86CjfYit',host='47.93.20.233',port=3306,db='aiya',charset='utf8') cursor = conn.cursor() sql="UPDATE ca3_yolo SET difference_score =%f WHERE frame_no = %d " %(float(difference_score), int(frame_num)); cursor.execute(sql) #sql="SELECT is_detected FROM captain_america3_sd WHERE frame_num ='%s' "% (frame_num); #cursor.execute(sql) cursor.rowcount conn.commit() cursor.close() def make_regalur_image(img, size = (256, 256)): return img.resize(size).convert('RGB') def split_image(img, part_size = (64, 64)): w, h = img.size pw, ph = part_size assert w % pw == h % ph == 0 return [img.crop((i, j, i+pw, j+ph)).copy() \ for i in xrange(0, w, pw) \ for j in xrange(0, h, ph)] def hist_similar(lh, rh): assert len(lh) == len(rh) return sum(1 - (0 if l == r else float(abs(l - r))/max(l, r)) for l, r in zip(lh, rh))/len(lh) def calc_similar(li, ri): # return hist_similar(li.histogram(), ri.histogram()) return sum(hist_similar(l.histogram(), r.histogram()) for l, r in zip(split_image(li), split_image(ri))) / 16.0 def calc_similar_by_path(lf, rf, frame_num): li, ri = make_regalur_image(Image.open(lf)), make_regalur_image(Image.open(rf)) difference_score = calc_similar(li, ri) ds_to_database(difference_score, frame_num) return difference_score def make_doc_data(lf, rf): li, ri = make_regalur_image(Image.open(lf)), make_regalur_image(Image.open(rf)) li.save(lf + '_regalur.png') ri.save(rf + '_regalur.png') fd = open('stat.csv', 'w') fd.write('\n'.join(l + ',' + r for l, r in zip(map(str, li.histogram()), map(str, ri.histogram())))) # print >>fd, '\n' # fd.write(','.join(map(str, ri.histogram()))) fd.close() import ImageDraw li = li.convert('RGB') draw = ImageDraw.Draw(li) for i in xrange(0, 256, 64): draw.line((0, i, 256, i), fill = '#ff0000') draw.line((i, 0, i, 256), fill = '#ff0000') li.save(lf + '_lines.png') def parse_args(): '''parse args''' parser = argparse.ArgumentParser() parser.add_argument('--image_path', default='/home/yanjieliu/my_opt/data_for_yolo/CAall/') parser.add_argument('--frame_num', default='370001') return parser.parse_args() if __name__ == '__main__': #path = r'test/TEST%d/%d.JPG' args=parse_args() print('%simage-%s.jpeg'%(args.image_path, args.frame_num)) start = time.time() for i in xrange(int(args.frame_num)+1, int(args.frame_num)+1000): print 'test_case: %.3f'%( \ calc_similar_by_path('%simage-%d.jpeg'%(args.image_path, i-1), '%simage-%s.jpeg'%(args.image_path, i), i)) #print 'test_case: %.3f'%( \ # calc_similar_by_path('%simage-%d.jpeg'%(args.image_path, int(args.frame_num)-1), '%simage-%s.jpeg'%(args.image_path, args.frame_num), args.frame_num)) endtime = time.time() print('time:\n') print str(endtime-start) # make_doc_data('test/TEST4/1.JPG', 'test/TEST4/2.JPG')
检测1000帧的差异度,用时293.609838009。。