Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import argparse
- import imutils
- import time
- import cv2
- import os
- from distanceDetect import distanceToCamera
- import paho.mqtt.client as mqtt
- from urllib.request import Request, urlopen
- mqttc = mqtt.Client()
- mqttc.connect("test.mosquitto.org", 1883, 60)
- # def getOutputsNames(net):
- # # Get the names of all the layers in the network
- # layersNames = net.getLayerNames()
- # # Get the names of the output layers, i.e. the layers with unconnected outputs
- # return [layersNames[i[0] - 1] for i in net.getUnconnectedOutLayers()]
- labelsPath = 'yolo/obj.names' #path to .names file
- LABELS = open(labelsPath).read().strip().split("\n")
- # initialize a list of colors to represent each possible class label
- np.random.seed(42)
- COLOURS = np.random.randint(0, 255, size=(len(LABELS), 3),
- dtype="uint8")
- weightsPath = 'yolo/yolov3-tiny-obj_2000.weights' #path to yolo weights file (2000?)
- configPath = 'yolo/yolov3-tiny-obj.cfg' #path to cfg file
- print("[INFO] loading YOLO from disk...")
- net = cv2.dnn.readNetFromDarknet(configPath, weightsPath)
- ln = net.getLayerNames()
- ln = [ln[i[0] - 1] for i in net.getUnconnectedOutLayers()]
- #ln = net.getOu
- ## ^^^ this determines teh output layer names from YOLO
- streamURL = "http://192.168.0.23:8080/video"
- stream = urlopen(streamURL)
- bytes = b""
- print('open stream')
- #vs = cv2.VideoCapture("http://937f8731.ngrok.io/stream") #fill in IP camera from ngrok
- writer = None
- (W, H) = (None, None)
- minConfidence = 0.60 #some value (scaled 0 - 1)
- NMSthreshold = 0.70 #Non maxima supression
- vs = cv2.VideoCapture(streamURL)
- while True:
- bytes += stream.read(1024)
- a = bytes.find(b'\xff\xd8')
- b = bytes.find(b'\xff\xd9')
- if a != -1 and b != -1:
- jpg = bytes[a:b+2]
- bytes = bytes[b+2:]
- frame = cv2.imdecode(np.fromstring(jpg, dtype=np.uint8), cv2.IMREAD_COLOR)
- print('here')
- (grabbed, frame) = vs.read()
- print('we have grabbed frame')
- print(grabbed)
- mqttc.publish("praxis3/test", 'frame')
- print('nani???')
- if not grabbed:
- print('grabbed')
- break
- # if the frame dimensions are empty, grab them
- if W is None or H is None:
- (W, H) = frame.shape[:2]
- #print('frame grabbed')
- #preprocess the image using blobFromImage
- blob = cv2.dnn.blobFromImage(frame, 1 / 255.0, (416, 416), swapRB=True, crop=False)
- #feed forward through the network
- net.setInput(blob)
- start = time.time()
- layerOutputs = net.forward(ln)
- end = time.time()
- boxes = []
- boxCenter = []
- confidences = []
- classIDs = []
- # loop over each of the layer outputs
- for output in layerOutputs:
- # loop over each of the detections
- for detection in output:
- # extract the class ID and confidence (i.e., probability)
- # of the current object detection
- scores = detection[5:]
- classID = np.argmax(scores)
- confidence = scores[classID]
- # filter out weak predictions by ensuring the detected
- # probability is greater than the minimum probability
- if confidence > minConfidence:
- # scale the bounding box coordinates back relative to
- # the size of the image, keeping in mind that YOLO
- # actually returns the center (x, y)-coordinates of
- # the bounding box followed by the boxes' width and
- # height
- box = detection[0:4] * np.array([W, H, W, H])
- (centerX, centerY, width, height) = box.astype("int")
- # use the center (x, y)-coordinates to derive the top
- # and and left corner of the bounding box
- x = int(centerX - (width / 2))
- y = int(centerY - (height / 2))
- # update our list of bounding box coordinates,
- # confidences, and class IDs
- boxes.append([x, y, int(width), int(height)])
- confidences.append(float(confidence))
- classIDs.append(classID)
- idxs = cv2.dnn.NMSBoxes(boxes, confidences, minConfidence, NMSthreshold)
- print('should be gucci')
- if len(idxs) > 0:
- # loop over the indexes we are keeping
- for i in idxs.flatten():
- # extract the bounding box coordinates
- (x, y) = (boxes[i][0], boxes[i][1])
- (w, h) = (boxes[i][2], boxes[i][3])
- (cX, cY) = (x + w/2, y + (y/2))
- sX, sY, sZ = frame.shape
- rX, rY = cX/sX, cY/sY
- d = distanceToCamera(w)
- mqttc.publish("praxis3/test", {'rX': rX, 'rY': rY, 'd': d})
- mqttc.publish("praxis3/test", 'helloworld ndhfh')
- mqttc.loop(2)
- if d > 30.48 :
- print('ROBOT HAS STOPPED')
- # load mode 2
- # draw a bounding box rectangle and label on the frame
- colour = [int(c) for c in COLOURS[classIDs[i]]]
- cv2.rectangle(frame, (x, y), (x + w, y + h), colour, 2)
- text = "{}: {:.4f}".format(LABELS[classIDs[i]], confidences[i])
- cv2.putText(frame, text, (x, y - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, colour, 2)
- # TODO: Show Images that have been bound!
Advertisement
Add Comment
Please, Sign In to add comment