Comments (1)
for anyone wondering here's the code for the working YOLOv3 object detection with webcam :
import time
import numpy as np
import cv2
import time
Load Yolo
#net = cv2.dnn.readNet("weight/yolov3-tiny.weights", "cfg/yolov3-tiny.cfg")
net = cv2.dnn.readNet("yolov3-tiny_training_last.weights", "yolov3-tiny_testing (1).cfg")
net.setPreferableBackend(cv2.dnn.DNN_BACKEND_OPENCV)
net.setPreferableTarget(cv2.dnn.DNN_TARGET_OPENCL)
classes = []
with open("obj.names", "r") as f:
classes = [line.strip() for line in f.readlines()]
layer_names = net.getLayerNames()
output_layers = [layer_names[i-1] for i in net.getUnconnectedOutLayers()]
colors = np.random.uniform(0, 255, size=(len(classes), 3)) #Generate Random Color
Load Video
cap = cv2.VideoCapture(0) #0 -> Webcam; "/path"
cap = cv2.imread("masker01132.jpg")
font = cv2.FONT_HERSHEY_SIMPLEX
timeframe = time.time()
frame_id = 0
maskx=0
masky=0
no_maskx=0
no_masky=0
while True:
_, frame = cap.read()
_, dots = cap.read()
frame_id += 1
height, width, channels = frame.shape
# Detecting objects
blob = cv2.dnn.blobFromImage(frame, 1/255, (320, 320), (0, 0, 0), True, crop=False)
net.setInput(blob)
outs = net.forward(output_layers)
# Showing informations on the screen
class_ids = []
confidences = []
boxes = []
for out in outs:
for detection in out:
scores = detection[5:]
class_id = np.argmax(scores)
confidence = scores[class_id]
if confidence > 0: #Confidence Level -> Accuracy
# Object detected
center_x = int(detection[0] * width)
center_y = int(detection[1] * height)
w = int(detection[2] * width)
h = int(detection[3] * height)
# Rectangle coordinates
x = int(center_x - w / 2)
y = int(center_y - h / 2)
boxes.append([x, y, w, h])
confidences.append(float(confidence))
class_ids.append(class_id)
indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.2, 0.4)
for i in range(len(boxes)):
if i in indexes:
x, y, w, h = boxes[i]
label = str(classes[class_ids[i]])
confidence = confidences[i]
# color = colors[i]
color = (255,255,255)
if label == "mask" :
no_maskx = 0
no_masky = 0
cv2.rectangle(frame, (x, y), (x + w, y + h), color, 2)
cv2.putText(frame, label, (x, y+30), font, 1, color, 2)
cv2.putText(frame, label + " " + str(round(confidence, 2)), (x, y+30), font, 1, color, 2)
center = ((x+w/2)-(width/2), (y+h/2)-(height/2))
maskx = (x+w/2)
masky = (y+h/2)
print (maskx,masky)
if label == "no_mask":
maskx = 0
masky = 0
cv2.rectangle(frame, (x, y), (x + w, y + h), color, 2)
cv2.putText(frame, label, (x, y+30), font, 1, color, 2)
cv2.putText(frame, label + " " + str(round(confidence, 2)), (x, y+30), font, 1, color, 2)
center = ((x+w/2)-(width/2), (y+h/2)-(height/2))
no_maskx = (x+w/2)
no_masky = (y+h/2)
print (no_maskx,no_masky)
elapsed_time = time.time() - timeframe
fps = frame_id / elapsed_time
cv2.putText(frame, str(round(fps,2)), (10, 50), font, 2, (255, 255, 255), 2) #FPS Value
cv2.putText(frame, "FPS", (220, 50), font, 2, (255, 255, 255), 2) #FPS Label
#frame_resize = cv2.resize(frame, (720,480), interpolation= cv2.INTER_LINEAR)
cv2.imshow("Image", frame)
cv2.circle(dots, (int(maskx),int(masky)), radius=2, color=(0, 0, 255), thickness=2)
cv2.circle(dots, (int(no_maskx),int(no_masky)), radius=2, color=(0, 0, 255), thickness=2)
cv2.putText(dots, str(round(fps,2)), (10, 50), font, 2, (255, 255, 255), 2) #FPS Value
cv2.putText(dots, "FPS", (220, 50), font, 2, (255, 255, 255), 2) #FPS Label
#dots_resize = cv2.resize(dots, (720,480), interpolation= cv2.INTER_LINEAR)
cv2.imshow("dotted Object", dots)
key = cv2.waitKey(1)
if key == 27: #Escape
break
cap.release()
cv2.destroyAllWindows()
from pykinect2.
Related Issues (20)
- Can't import utils_PyKinectV2
- Related to 1.getting same values of pixel in entire column 2. frame resolution HOT 3
- except COMError, err: SyntaxError: invalid syntax HOT 4
- Kinectv2 and python
- PyGame Examples Run but no Image is Display HOT 1
- pykinectv2报错NameError: name 'unicode' is not defined
- Anyone can provide video tutorial on setup? HOT 3
- Could not find module 'Kinect20' (or one of its dependencies). Try using the full path with constructor syntax
- argument 4: <class 'TypeError'>: expected LP__CameraSpacePoint instance instead of LP__CameraSpacePoint
- distortion and tilt HOT 5
- Picture from multiple kinects HOT 4
- How do i get the Euler Angle of a joint? HOT 1
- kinectv1 and python HOT 1
- PyKinectV2 name 'comtypes' is not defined HOT 1
- Import issue with PyKinectV2 HOT 2
- -
- Could not find module 'Kinect20' (or one of its dependencies). HOT 1
- black screen when opening HOT 7
- Body is not being detected HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pykinect2.