Try this... updated their code....works on my Pi5...
The main issue is that Videocapture() no longer works with pi cameras. so I used another method.
Download and unzip their zip file and put the directory Object_Detection_Files under Desktop.
Always press ESC to exit...
The main issue is that Videocapture() no longer works with pi cameras. so I used another method.
Download and unzip their zip file and put the directory Object_Detection_Files under Desktop.
Always press ESC to exit...
Code:
#Import the Open-CV extra functionalitiesimport cv2import globimport subprocessimport osimport signal# find userusers = []users.append(os.getlogin())#This is to pull the information about what each object is calledclassNames = []classFile = "/home/" + users[0] + "/Desktop/Object_Detection_Files/coco.names"with open(classFile,"rt") as f: classNames = f.read().rstrip("\n").split("\n")#This is to pull the information about what each object should look likeconfigPath = "/home/"+ users[0] + "/Desktop/Object_Detection_Files/ssd_mobilenet_v3_large_coco_2020_01_14.pbtxt"weightsPath = "/home/"+ users[0] + "/Desktop/Object_Detection_Files/frozen_inference_graph.pb"#This is some set up values to get good resultsnet = cv2.dnn_DetectionModel(weightsPath,configPath)net.setInputSize(320,320)net.setInputScale(1.0/ 127.5)net.setInputMean((127.5, 127.5, 127.5))net.setInputSwapRB(True)#This is to set up what the drawn box size/colour is and the font/size/colour of the name tag and confidence label def getObjects(img, thres, nms, draw=True, objects=[]): classIds, confs, bbox = net.detect(img,confThreshold=thres,nmsThreshold=nms)#Below has been commented out, if you want to print each sighting of an object to the console you can uncomment below #print(classIds,bbox) if len(objects) == 0: objects = classNames objectInfo =[] if len(classIds) != 0: for classId, confidence,box in zip(classIds.flatten(),confs.flatten(),bbox): className = classNames[classId - 1] if className in objects: objectInfo.append([box,className]) if (draw): cv2.rectangle(img,box,color=(0,255,0),thickness=2) cv2.putText(img,classNames[classId-1].upper(),(box[0]+10,box[1]+30), cv2.FONT_HERSHEY_COMPLEX,1,(0,255,0),2) cv2.putText(img,str(round(confidence*100,2)),(box[0]+200,box[1]+30), cv2.FONT_HERSHEY_COMPLEX,1,(0,255,0),2) return img,objectInfo#Below determines the size of the live feed window that will be displayed on the Raspberry Pi OSif __name__ == "__main__": # start Pi camera files = glob.glob('/run/shm/*.jpg') for f in files: os.remove(f) rpistr = "rpicam-vid --camera 0 -n -t 0 --framerate 25 --segment 1 --codec mjpeg --width 640 --height 480 -o /run/shm/test%06d.jpg " p = subprocess.Popen(rpistr, shell=True, preexec_fn=os.setsid) #Below is the never ending loop that determines what will happen when an object is identified. while True: # wait for enough images pics = glob.glob('/run/shm/test*.jpg') while len(pics) < 2: pics = glob.glob('/run/shm/test*.jpg') pics.sort(reverse=True) # GET AN IMAGE from Pi camera img = cv2.imread(pics[1]) #Below provides a huge amount of controll. the 0.45 number is the threshold number, the 0.2 number is the nms number) result, objectInfo = getObjects(img,0.45,0.2) #print(objectInfo) cv2.imshow("Output",img) # remove unused Pi camera images from RAM for x in range(1,len(pics)): os.remove(pics[x]) k = cv2.waitKey(500) if k == 27: # Esc key to stop # EXIT os.killpg(p.pid, signal.SIGTERM) cv2.destroyAllWindows() break
- Image may be NSFW.
Clik here to view.
Statistics: Posted by gordon77 — Thu Jan 11, 2024 10:50 am