def object_detector(img): dark_frame = pydarknet.Image(img) results = net.detect(dark_frame) del dark_frame op = list() for cat, score, bounds in results: x, y, w, h = bounds op.append([x, y, w, h]) return op
def yolo(): # retrieve parameters from html form filename1 = request.form['image'] yversion = request.form['yolo_version'] # open images target = os.path.join(APP_ROOT, 'static/images') destination1 = "/".join([target, filename1]) img1 = Image.open(destination1) img = np.asarray(img1) img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR) destination = "/".join([target, 'temp.png']) if os.path.isfile(destination): print("Existing file found at " + destination) os.remove(destination) print("Performing YOLO" + yversion + " on image at " + destination1 + "...\nThis might take a while...") start_time = time.time() results = darknet[yversion].detect(pydarknet.Image(img)) end_time = time.time() #print(results) print("Elapsed Time:", end_time - start_time) print("Detected:") for cat, score, bounds in results: x, y, w, h = bounds label = str(cat.decode("utf-8")) conf = float(score * 100) displayText = str("{}, {:.2f}%".format(label, conf)) cv2.rectangle(img, (int(x - w / 2), int(y - h / 2)), (int(x + w / 2), int(y + h / 2)), (0, 0, 255), thickness=1) cv2.putText(img, displayText, (int(x - w / 2), int(y - h / 2)), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), thickness=1, lineType=cv2.LINE_AA) print("> " + displayText) # save and return image img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) im_pil = Image.fromarray(img) im_pil.save(destination) return send_image('temp.png')
def predict(self, image: Image) -> List[Prediction]: """ Predict bounding boxes for the persons in the image. :param image: the PIL image :return: the bounding boxes for the persons in the image. """ img = numpy.asarray(image) img_darknet = pydarknet.Image(img) detections = self._model.detect(img_darknet) predictions = self._get_predictions_from_detections(detections) return list(predictions)
def on_msg_camera_stream(self, msg): self._logger.info('%s received frame %s', self.name, msg.timestamp) start_time = time.time() cv_img = self.bridge.imgmsg_to_cv2(msg.data, 'rgb8') img = pydarknet.Image(cv_img) results = self._net.detect(img) #self.add_bounding_boxes(cv_img, results) # bb_file = open('images/bounding_boxes{}'.format(msg.timestamp.coordinates[1]), 'wb') # pickle.dump(results, bb_file) # bb_file.close() # output_msg = Message((msg.data, results), msg.timestamp) runtime = (time.time() - start_time) * 1000 self._logger.info('Object detector {} runtime {}'.format( self.name, runtime)) output_msg = Message((results, runtime), msg.timestamp) self.get_output_stream(self._output_stream_name).send(output_msg)
def find_bounds_in_image(image_frame, darknet_model, class_label): darknet_image_frame = pydarknet.Image(image_frame) results = darknet_model.detect( darknet_image_frame, thresh=DETECTION_THRESHOLD, hier_thresh=.5, nms=.45) # todo: change this thresh-params thresh=0.01 #0.00051, results_bounds = [] for category, score, bounds in results: category = str(category.decode("utf-8")) if category.lower() in class_label: x, y, w, h = bounds y1, y2 = int(y - h / 2), int(y + h / 2) x1, x2 = int(x - w / 2), int(x + w / 2) image_box_size_ratio = get_box_size_ratio( y1, y2, x1, x2, image_shape=image_frame.shape) if image_box_size_ratio < BOUND_RATIO_THRESHOLD: results_bounds.append((y1, y2, x1, x2)) return results_bounds
def find_in_img(image_frame, darknet_model, class_labels, thresh=0.5, bound_size_thresh=0.5): darknet_image_frame = pydarknet.Image(image_frame) results = darknet_model.detect( darknet_image_frame, thresh=thresh, # 0.00051, hier_thresh=.5, nms=.45) # todo: change this thresh-params thresh=0.01 #0.00051, results_bounds = [] for category, score, bounds in results: category = str(category.decode("utf-8")) if category.lower() in [label.lower() for label in class_labels]: x, y, w, h = bounds y1, y2 = int(y - h / 2), int(y + h / 2) x1, x2 = int(x - w / 2), int(x + w / 2) if box_size(y1, y2, x1, x2, image_shape=image_frame.shape) < bound_size_thresh: results_bounds.append((y1, y2, x1, x2)) return results_bounds
net = pydarknet.Detector(bytes(f"{DARKNET_LOCATION}/cfg/horsey-yolov3-tiny2.cfg", encoding="utf-8"), bytes(f"{DARKNET_LOCATION}/backup/horsey-yolov3-tiny2_5500.weights", encoding="utf-8"), 0, bytes(f"{DARKNET_LOCATION}/data/horsey-obj.data", encoding="utf-8")) """ # label a single image if sys.argv[1] == "-i": filepath = sys.argv[2] img = cv2.imread(filepath) # time the classification start_time = time.time() # Use YOLO Darknet to classify the image and store the results img_darknet = pydarknet.Image(img) results = net.detect(img_darknet) end_time = time.time() print(f"Time to classify frame: {end_time-start_time}") for category, score, bounds in results: # Bounds are BoundingBox coordinates, with x,y the centre, and w,h as width and height x, y, w, h = bounds # Label as found be YOLO CNN label = str(category.decode("utf-8")) # Draw Bounding Boxes around found RoIs, with different colours depending on found label/class if label == "horse": cv2.rectangle(img, (int(x - w / 2), int(y - h / 2)), (int(x + w / 2), int(y + h / 2)), (255, 0, 0),