How can I capture detected image of object Yolov3 and display in flask

2024/11/16 15:25:11

I am working on Real Time Object Detection using YOLOv3 with OpenCV and Python. It's works well. Currently I try to capture detected image of object and display in flask. Do someone know how to implement this features? Hope someone can helps. I got the tutorial based on this link https://medium.com/analytics-vidhya/real-time-object-detection-using-yolov3-with-opencv-and-python-64c985e14786

import cv2
import numpy as np
import os
import time
import detect as dt
from PIL import ImagelabelsPath = os.path.sep.join(["yolo-coco", "coco.names"])
weightsPath = os.path.sep.join(["yolo-coco", "yolov3.weights"])
configPath = os.path.sep.join(["yolo-coco", "yolov3.cfg"])labelsPath = os.path.sep.join(["yolo-coco", "coco.names"])
LABELS = open(labelsPath).read().strip().split("\n")net = cv2.dnn.readNet(configPath, weightsPath)layer_names = net.getLayerNames()
outputlayers = [layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()]colors= np.random.uniform(0,255,size=(len(LABELS),3))#loading image
cap0=cv2.VideoCapture(0) #0 for 1st webcam
cap1=cv2.VideoCapture(1)
font = cv2.FONT_HERSHEY_PLAIN
starting_time= time.time()
frame_id = 0
count = 0
use_cuda = 1
# configPath.cuda()while True:ret0,frame0= cap0.read() # ret1,frame1= cap1.read() # image = cv2.cvtColor(frame0, cv2.COLOR_BGR2RGB)im_pil = Image.fromarray(image)im_pil = im_pil.resize((200, 200))boxes = dt.do_detect(image, im_pil, 0.5, 0.4, use_cuda)if (ret0):frame_id+=1#print(frame_id)height,width,channels = frame0.shape#print (frame.shape)#detecting objectsblob = cv2.dnn.blobFromImage(frame0,0.00392,(320,320),(0,0,0),True,crop=False) #reduce 416 to 320    net.setInput(blob)outs = net.forward(outputlayers)#print(outs)print(outs[1])#Showing info on screen/ get confidence score of algorithm in detecting an object in blobclass_ids=[]confidences=[]boxes=[]for out in outs:#print(out)for detection in out:scores = detection[5:]class_id = np.argmax(scores)confidence = scores[class_id]print(confidence)if confidence > 0.3:#onject detectedcenter_x= int(detection[0]*width)center_y= int(detection[1]*height)w = int(detection[2]*width)h = int(detection[3]*height)#cv2.circle(img,(center_x,center_y),10,(0,255,0),2)#rectangle co-ordinatersx=int(center_x - w/2)y=int(center_y - h/2)#cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)boxes.append([x,y,w,h]) #put all rectangle areasconfidences.append(float(confidence)) #how confidence was that object detected and show that percentageclass_ids.append(class_id) #name of the object tha was detectedindexes = cv2.dnn.NMSBoxes(boxes,confidences,0.4,0.6)result = open('C:/Users/HP/Miniconda3/envs/count_vechicle/coding/images/frame%04d.txt'%(count), 'w')for i in range(len(boxes)):result.write(boxes[i])count = count + 1success, image = vidcap.read()result.close()if i in indexes:x,y,w,h = boxes[i]label = str(LABELS[class_ids[i]])confidence= confidences[i]color = colors[class_ids[i]]cv2.rectangle(frame0,(x,y),(x+w,y+h),color,2)cv2.putText(frame0,label+" "+str(round(confidence,2)),(x,y+30),font,1,(255,255,255),2)elapsed_time = time.time() - starting_timefps=frame_id/elapsed_timecv2.putText(frame0,"FPS:"+str(round(fps,2)),(10,50),font,2,(0,0,0),1)cv2.imshow("Image0",frame0)key = cv2.waitKey(1) #wait 1ms the loop will start again and we will process the next frameif (ret1):cv2.imshow("Image1",frame1)key = cv2.waitKey(1) #wait 1ms the loop will start again and we will process the next frameif key == 27: #esc key stops the processbreak;cap0.release()    
cap1.release()
cv2.destroyAllWindows()
Answer

Using the bounding box coordinates of the detected object, you can crop a new image out of it and then save it to display.

Try this:

# extract the bounding box coordinates
(x, y) = (boxes[i][0], boxes[i][1])
(w, h) = (boxes[i][2], boxes[i][3])
crop_img = frame[y:y + h, x:x + w] #frame of video you are looping through
cv2.imwrite(<filename>, crop_img)

Update: according to your code :

if i in indexes:x,y,w,h = boxes[i]crop_img = frame0[y:y + h, x:x + w]cv2.imwrite(<filename>, crop_img)

Using the box coordinates you can crop that part

Hope it helps.

https://en.xdnf.cn/q/118918.html

Related Q&A

ValueError: Shapes (2, 1) and () are incompatible

I have to use Tensorflow 0.11 for this code repo and this is the error I get:(py35) E:\opensource_codes\gesture_recognition\hand3d-master>python run.py Traceback (most recent call last):File "r…

Subtotals for Pandas pivot table index and column

Id like to add subtotal rows for index #1 (ie. Fruits and Animal) and subtotal columns for columns (ie. 2015 and 2016).For the subtotal columns, I could do something like this, but it seems inefficient…

Create two new columns derived from original columns in Python

I have a dataframe, df, that contains two columns that contain quarter values. I would like to create two more columns that are the equivalent "long dates". Data ID Quarter Delivery A …

In dataframe: how to pull minutes and seconds combinedly(mm:ss) from timedelta using python

i have already tried these but by this we can only get hour/minute/second but not both minute and second In[7]: df[B] = df[A].dt.components[hours] df Out[7]:A B0 02:00:00 2 1 01:00:00 1from this tim…

Understanding python numpy syntax to mask and filter array

Please help me understand how the lines below are working.How does a pair of parentheses create an array and then individual elements go through logical condition check and create a new array? How doe…

How to get the proper link from a website using python beautifulsoup?

When I try to scrape roster links, I get https://gwsports.com/roster.aspx?path=wpolo when I open it on chrome it changes to https://gwsports.com/sports/mens-water-polo/roster. I want to scrape it in p…

tkinter frame propagate not behaving?

If you uncomment the options_frame_title you will see that it does not behave properly. Am I missing something? That section was just copied and pasted from the preview_frame_title and that seems to h…

python modules installing Error Visual c++ 14.0 is required [duplicate]

This question already has answers here:pip install ecos errors with "Microsoft Visual C++ 14.0 is required." [duplicate](1 answer)Error "Microsoft Visual C++ 14.0 is required (Unable to …

How to render Flask Web App with Javascript [duplicate]

This question already has answers here:Return JSON response from Flask view(15 answers)How to append ajax html response to next to current div(5 answers)Closed 5 years ago.Edit: Hi Ive checked the dupl…

Use start and stop function with same button in Tkinter

With the help of the command button, I am able to disconnect the frame in Tkinter. But is there any way which helps to use the same button to start also?import tkinter as tk counter = 0 def counter_la…