from imageai.Detection import ObjectDetection
import os
import cv2
import hashlib
user,pwd,ip,channel = 'admin','12345','192.168.1.123',1
video_stream_path = 'rtsp://%s:%s@%s/h265/ch%s/main/av_stream' % (user,pwd,ip,channel)
def getmd5(frame):
m = hashlib.md5()
m.update(frame)
md5code = m.hexdigest()
#print(md5code)
return md5code
#faceCascad = cv2.CascadeClassifier("C:/opencv/sources/data/haarcascades/haarcascade_frontalface_default.xml")
execution_path = os.getcwd()
detector = ObjectDetection()
detector.setModelTypeAsRetinaNet()
detector.setModelPath(os.path.join(execution_path,"resnet50_coco_best_v2.0.1.h5"))
detector.loadModel()
cap=cv2.VideoCapture(video_stream_path)
while(1):
ret,frame=cap.read()
frame2 = cv2.resize(frame,(1024,768))
cv2.imshow('Object',frame2)
k = cv2.waitKey(1) & 0xff
if k==27:
break
elif k == ord('s'):
#cv2.imwrite(md5+'.png',frame)
cv2.imwrite('fa5.jpg',frame)
detections = detector.detectObjectsFromImage(
input_image=os.path.join(execution_path ,"fa5.jpg"),
output_image_path=os.path.join(execution_path,"imagenew.jpg"))
for eachObject in detections:
print(str(eachObject["name"]) + ":" + str(eachObject["percentage_probability"]) )
frame2 = cv2.imread("imagenew.jpg")
cv2.imshow('Object',frame2)
k = cv2.waitKey(0) & 0xff
if k==27:
break
elif k == ord('s'):
cv2.imwrite(getmd5(frame2)+'.png',frame2)
cap.release()
cv2.destroyAllWindows()