from imageai.Detection import VideoObjectDetection
from imageai.Detection import ObjectDetection
import os
import cv2
execution_path = os.getcwd()
video_file_path = './20190925_13_59_44_811626_admin_张大帅_060757_毕业论文写作技巧_teacher_full.mp4'
detected_file_path = video_file_path.strip('.mp4')+'_camera_detected_video'
imdetector = ObjectDetection()
imdetector.setModelTypeAsYOLOv3()
imdetector.setModelPath( os.path.join(execution_path , "yolo.h5"))
imdetector.loadModel()
detector = VideoObjectDetection()
detector.setModelTypeAsYOLOv3()
detector.setModelPath(os.path.join(execution_path , "yolo.h5"))
detector.loadModel()
#detector.loadModel(detection_speed = 'flash')
#video="http://admin:admin@192.168.1.105:8081/" #此处@后的ipv4 地址需要修改
#camera = cv2.VideoCapture(video) #可使用参数video访问网络视频
#cap = cv2.VideoCapture('./20190925_13_59_44_811626_admin_张大帅_060757_毕业论文写作技巧_teacher_full.mp4')
#def forFrame(frame_number,
# output_array,
# output_count,
# returned_frame
# ):
def forFrame(frame_number, output_array, output_count, returned_frame):
global video_file_path
global detected_file_path
#cv2.imshow('camera',returned_frame)
#cv2.waitKey(1)
#print('frame_number:',frame_number)
#print('output_array:',output_array)
#print('output_count:',output_count)
try:
if output_count['person']>=1:
print(output_count['person'])
print('output_array:',output_array)
with open('./video-detected'+detected_file_path+'_output_array_log.txt','at') as f:
f.write(str(output_array)+'\n')
for eachObject in output_array:
print(eachObject['name'],eachObject['percentage_probability'],eachObject['box_points'][:2])
print("--------------------------------")
print('output_count:',output_count)
with open('./video-detected'+detected_file_path+'_output_count_log.txt','at') as f:
f.write(str(output_count)+'\n')
if output_count['person']>=1:
#print('1')
impath='img'+str(frame_number)+'.jpg'
cv2.imwrite('./img/'+impath,returned_frame)
imdetections = imdetector.detectObjectsFromImage(
input_image=os.path.join(execution_path,'img/'+impath),
output_image_path=os.path.join(execution_path,'img/'+'new'+impath))
#print(2)
print(imdetections)
for eachObject in imdetections:
print(eachObject['name'],eachObject['percentage_probability'],eachObject['box_points'][:2])
print("--------------------------------")
cv2.putText(returned_frame, 'Focus:'+str(eachObject['percentage_probability']), eachObject['box_points'][:2], cv2.FONT_HERSHEY_PLAIN, 2.0, (0, 0, 255), 2)
cv2.putText(returned_frame, 'Focus', (40,50), cv2.FONT_HERSHEY_PLAIN, 2.0, (0, 0, 255), 2)
#print(3)
cv2.imshow('camera',returned_frame)
cv2.waitKey(1)
except:
pass
def videoDtection(input_file_path=video_file_path):
global video_file_path
global detected_file_path
video_file_path = input_file_path
detected_file_path = video_file_path.strip('.mp4')+'_camera_detected_video'
video_detected_path = detector.detectObjectsFromVideo(
input_file_path='./video'+input_file_path, #'./20190925_13_59_44_811626_admin_张大帅_060757_毕业论文写作技巧_teacher_full.mp4',#camera_input=cap,
output_file_path='./video-detected'+detected_file_path, #os.path.join(execution_path, "camera_detected_video"),
frames_per_second=20,
per_frame_function=forFrame,
log_progress=False,
minimum_percentage_probability=30,
return_detected_frame=True,
frame_detection_interval=5,
display_percentage_probability=False,
display_object_name=False)
return video_detected_path #'./video-detected'+detected_file_path
if __name__ == "__main__":
videoDtection('./20190925_13_59_44_811626_admin_张大帅_060757_毕业论文写作技巧_teacher_full.mp4')