def get_boxes(opts, outs, source_size, num=5, threshold=0.1): assert box_constructor is not None, '`get_boxes` requires `darkflow`.' h, w = source_size boxes = [[] for _ in xrange(opts['classes'])] opts['num'] = num opts['thresh'] = threshold opts['out_size'] = list(outs[0].shape) results = box_constructor(opts, outs[0].copy()) for b in results: max_idx = np.argmax(b.probs) max_score = b.probs[max_idx] if max_score > threshold: x1 = int((b.x - b.w / 2) * w) y1 = int((b.y - b.h / 2) * h) x2 = int((b.x + b.w / 2) * w) y2 = int((b.y + b.h / 2) * h) if x1 < 0: x1 = 0 if x2 > w - 1: x2 = w - 1 if y1 < 0: y1 = 0 if y2 > h - 1: y2 = h - 1 boxes[max_idx].append((x1, y1, x2, y2, max_score)) for i in xrange(opts['classes']): boxes[i] = np.asarray(boxes[i], dtype=np.float32) return boxes
def findboxes(self, net_out): # meta meta = self.meta boxes = list() boxes = box_constructor(meta, net_out) return boxes
def yolo_boxes(opts, outs, source_size): assert box_constructor is not None, '`get_boxes` requires `darkflow`.' h, w = source_size threshold = opts['thresh'] boxes = [[] for _ in xrange(opts['classes'])] results = box_constructor(opts, outs[0].copy()) for b in results: max_indx = np.argmax(b.probs) max_prob = b.probs[max_indx] if max_prob > threshold: left = int((b.x - b.w / 2) * w) right = int((b.x + b.w / 2) * w) top = int((b.y - b.h / 2) * h) bot = int((b.y + b.h / 2) * h) if left < 0: left = 0 if right > w - 1: right = w - 1 if top < 0: top = 0 if bot > h - 1: bot = h - 1 boxes[max_indx].append((left, top, right, bot, max_prob)) for i in xrange(opts['classes']): boxes[i] = np.asarray(boxes[i], dtype=np.float32) return boxes
def getResults(requestData): with open(os.path.join("./built_graph/yolov2.meta"), 'r') as fp: meta = json.load(fp) imgdata = base64.b64decode(requestData["data"]) data = Image.open(io.BytesIO(imgdata)) image_np = cv2.cvtColor(np.array(data), cv2.COLOR_BGR2RGB) h, w, c = meta['inp_size'] imsz = cv2.resize(image_np , (w, h)) imsz = imsz / 255. imsz = imsz[:,:,::-1] image_np_expanded = np.expand_dims(imsz, 0) [h, w] = image_np.shape[:2] headers = {'Accept': 'application/octet-stream', 'content-type': 'application/json'} start_time1 = time.time() r = requests.post(url = URL , data = json.dumps(requestData), headers = headers) elapsed_time1 = time.time() - start_time print("Time to get the results : ", elapsed_time1) data = r.json() data1 = ast.literal_eval(data) out1 = data1["detection"] boxes = box_constructor(meta, np.array(out1, dtype=np.float32)) #print(boxes) boxesInfo = list() #resultsForJSON = [] #colors = meta['colors'] threshold = meta['thresh'] for b in boxes: max_indx = np.argmax(b.probs) max_prob = b.probs[max_indx] label = meta['labels'][max_indx] if max_prob > threshold: left = int ((b.x - b.w/2.) * w) right = int ((b.x + b.w/2.) * w) top = int ((b.y - b.h/2.) * h) bot = int ((b.y + b.h/2.) * h) if left < 0 : left = 0 if right > w - 1: right = w - 1 if top < 0 : top = 0 if bot > h - 1: bot = h - 1 mess = '{}'.format(label) boxResults = (left, right, top, bot, mess, max_indx, max_prob) print(boxResults)
def detect_frame(self, img): # print("[Yitao] in yolo.py:detect_frame() is called...!") # return self.tfnet.return_predict(img) # Yitao-TLS-Begin h, w, _ = img.shape im = resize_input(img) this_inp = np.expand_dims(im, 0) request = predict_pb2.PredictRequest() request.model_spec.name = 'darkflow_yolo' request.model_spec.signature_name = 'predict_images' request.inputs['input'].CopyFrom( tf.contrib.util.make_tensor_proto(this_inp, dtype=np.float32, shape=this_inp.shape)) result = self.stub.Predict(request, 10.0) # 10 secs timeout tmp = result.outputs['output'] tt = tensor_util.MakeNdarray(tmp)[0] # print(tt[:,0,0]) # print(tt[:,0,1]) meta = { 'labels': [ 'person', 'bicycle', 'car', 'motorbike', 'aeroplane', 'bus', 'train', 'truck', 'boat', 'traffic light', 'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow', 'elephant', 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee', 'skis', 'snowboard', 'sports ball', 'kite', 'baseball bat', 'baseball glove', 'skateboard', 'surfboard', 'tennis racket', 'bottle', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple', 'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair', 'sofa', 'pottedplant', 'bed', 'diningtable', 'toilet', 'tvmonitor', 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone', 'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors', 'teddy bear', 'hair drier', 'toothbrush' ], u'jitter': 0.3, u'anchors': [ 0.57273, 0.677385, 1.87446, 2.06253, 3.33843, 5.47434, 7.88282, 3.52778, 9.77052, 9.16828 ], u'random': 1, 'colors': [(254, 254, 254), (254, 254, 127), (254, 254, 0), (254, 254, -127), (254, 254, -254), (254, 127, 254), (254, 127, 127), (254, 127, 0), (254, 127, -127), (254, 127, -254), (254, 0, 254), (254, 0, 127), (254, 0, 0), (254, 0, -127), (254, 0, -254), (254, -127, 254), (254, -127, 127), (254, -127, 0), (254, -127, -127), (254, -127, -254), (254, -254, 254), (254, -254, 127), (254, -254, 0), (254, -254, -127), (254, -254, -254), (127, 254, 254), (127, 254, 127), (127, 254, 0), (127, 254, -127), (127, 254, -254), (127, 127, 254), (127, 127, 127), (127, 127, 0), (127, 127, -127), (127, 127, -254), (127, 0, 254), (127, 0, 127), (127, 0, 0), (127, 0, -127), (127, 0, -254), (127, -127, 254), (127, -127, 127), (127, -127, 0), (127, -127, -127), (127, -127, -254), (127, -254, 254), (127, -254, 127), (127, -254, 0), (127, -254, -127), (127, -254, -254), (0, 254, 254), (0, 254, 127), (0, 254, 0), (0, 254, -127), (0, 254, -254), (0, 127, 254), (0, 127, 127), (0, 127, 0), (0, 127, -127), (0, 127, -254), (0, 0, 254), (0, 0, 127), (0, 0, 0), (0, 0, -127), (0, 0, -254), (0, -127, 254), (0, -127, 127), (0, -127, 0), (0, -127, -127), (0, -127, -254), (0, -254, 254), (0, -254, 127), (0, -254, 0), (0, -254, -127), (0, -254, -254), (-127, 254, 254), (-127, 254, 127), (-127, 254, 0), (-127, 254, -127), (-127, 254, -254)], u'num': 5, u'thresh': self.threshold, 'inp_size': [608, 608, 3], u'bias_match': 1, 'out_size': [19, 19, 425], 'model': '/home/yitao/Documents/fun-project/darknet-repo/darkflow/cfg/yolo.cfg', u'absolute': 1, 'name': 'yolo', u'coord_scale': 1, u'rescore': 1, u'class_scale': 1, u'noobject_scale': 1, u'object_scale': 5, u'classes': 80, u'coords': 4, u'softmax': 1, 'net': { u'hue': 0.1, u'saturation': 1.5, u'angle': 0, u'decay': 0.0005, u'learning_rate': 0.001, u'scales': u'.1,.1', u'batch': 1, u'height': 608, u'channels': 3, u'width': 608, u'subdivisions': 1, u'burn_in': 1000, u'policy': u'steps', u'max_batches': 500200, u'steps': u'400000,450000', 'type': u'[net]', u'momentum': 0.9, u'exposure': 1.5 }, 'type': u'[region]' } boxes = list() boxes = box_constructor(meta, tt) # for box in boxes: # print("(%s, %s, %s, %s) with class_num = %s, probs = %s" % (str(box.x), str(box.y), str(box.w), str(box.h), str(box.class_num), str(box.probs))) boxesInfo = list() for box in boxes: tmpBox = process_box(box, h, w, self.threshold, meta) if tmpBox is None: continue boxesInfo.append({ "label": tmpBox[4], "confidence": tmpBox[6], "topleft": { "x": tmpBox[0], "y": tmpBox[2] }, "bottomright": { "x": tmpBox[1], "y": tmpBox[3] } }) return boxesInfo
def findboxes_meta(meta, net_out): boxes = list() boxes = box_constructor(meta, net_out) return boxes