ObjectDetection


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()

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值