GithubHelp home page GithubHelp logo

PyKinect2 Integration wih OpenCV for YOLOv3 throws an error of : Number of input channels should be multiple of 3 but got 4 in function about pykinect2 HOT 1 OPEN

Yudh0B avatar Yudh0B commented on August 11, 2024
PyKinect2 Integration wih OpenCV for YOLOv3 throws an error of : Number of input channels should be multiple of 3 but got 4 in function

from pykinect2.

Comments (1)

Yudh0B avatar Yudh0B commented on August 11, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.