Spaces:
Configuration error
Configuration error
Commit ·
9b11319
1
Parent(s): 2b0ea86
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from google.colab.patches import cv2_imshow
|
| 4 |
+
thres = 0.45 # Threshold to detect object
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def Detection(filename):
|
| 8 |
+
cap = cv2.VideoCapture(filename)
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
cap.set(3,1280)
|
| 12 |
+
cap.set(4,720)
|
| 13 |
+
cap.set(10,70)
|
| 14 |
+
|
| 15 |
+
error="NoneType' object has no attribute"
|
| 16 |
+
classNames= []
|
| 17 |
+
FinalItems=[]
|
| 18 |
+
classFile = 'coco.names'
|
| 19 |
+
with open(classFile,'rt') as f:
|
| 20 |
+
#classNames = f.read().rstrip('n').split('n')
|
| 21 |
+
classNames = f.readlines()
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
# remove new line characters
|
| 25 |
+
classNames = [x.strip() for x in classNames]
|
| 26 |
+
print(classNames)
|
| 27 |
+
configPath = 'ssd_mobilenet_v3_large_coco_2020_01_14.pbtxt'
|
| 28 |
+
weightsPath = 'frozen_inference_graph.pb'
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
net = cv2.dnn_DetectionModel(weightsPath,configPath)
|
| 32 |
+
net.setInputSize(320,320)
|
| 33 |
+
net.setInputScale(1.0/ 127.5)
|
| 34 |
+
net.setInputMean((127.5, 127.5, 127.5))
|
| 35 |
+
net.setInputSwapRB(True)
|
| 36 |
+
|
| 37 |
+
while True:
|
| 38 |
+
success,img = cap.read()
|
| 39 |
+
try:
|
| 40 |
+
classIds, confs, bbox = net.detect(img,confThreshold=thres)
|
| 41 |
+
except:
|
| 42 |
+
pass
|
| 43 |
+
print(classIds,bbox)
|
| 44 |
+
try:
|
| 45 |
+
if len(classIds) != 0:
|
| 46 |
+
for classId, confidence,box in zip(classIds.flatten(),confs.flatten(),bbox):
|
| 47 |
+
|
| 48 |
+
#cv2.rectangle(img,box,color=(0,255,0),thickness=2)
|
| 49 |
+
#cv2.putText(img,classNames[classId-1].upper(),(box[0]+10,box[1]+30),
|
| 50 |
+
#cv2.FONT_HERSHEY_COMPLEX,1,(0,255,0),2)
|
| 51 |
+
#cv2.putText(img,str(round(confidence*100,2)),(box[0]+200,box[1]+30),
|
| 52 |
+
#cv2.FONT_HERSHEY_COMPLEX,1,(0,255,0),2)
|
| 53 |
+
if FinalItems.count(classNames[classId-1]) == 0:
|
| 54 |
+
FinalItems.append(classNames[classId-1])
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
cv2_imshow(img)
|
| 58 |
+
cv2.waitKey(10)
|
| 59 |
+
except Exception as err:
|
| 60 |
+
print(err)
|
| 61 |
+
t=str(err)
|
| 62 |
+
if t.__contains__(error):
|
| 63 |
+
break
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
print(FinalItems)
|
| 67 |
+
return str(FinalItems)
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
interface = gr.Interface(fn=Detection,
|
| 71 |
+
inputs=["video"],
|
| 72 |
+
outputs="text",
|
| 73 |
+
title='Object Detection in Video')
|
| 74 |
+
interface.launch()
|