def preprocessImages(self, imagelist): net = Detector(bytes(cfgpath, encoding="utf-8"), bytes(weightpath, encoding="utf-8"), 0, bytes(objpath, encoding="utf-8")) fins = [] flukes = [] #process imagewise for i in imagelist: img = cv2.imread(i) img_darknet = Image(img) results = net.detect(img_darknet) #go through each result for the image for cat, score, bounds in results: label = str(cat).split("'")[1] #clean the label x, y, w, h = bounds #define the cropped area y1 = max(int(y - h / 2), 0) y2 = min(int(y + h / 2), img.shape[0]) x1 = max(int(x - w / 2), 0) x2 = min(int(x + w / 2), img.shape[1]) if label == "fin": fins.append([i, [x1, y1, x2, y2]]) elif label == "fluke": flukes.append([i, [x1, y1, x2, y2]]) return (fins, flukes)
def darknet_preprocess(self, images): # resize the images to shorter edge be neth neth = netw = self.resolution im_h = images.shape[1] im_w = images.shape[2] if (1.0 * netw / im_w) < (1.0 * neth / im_h): new_w = netw new_h = (im_h * netw) // im_w else: new_h = neth new_w = (im_w * neth) // im_h images = resize_images(images, [new_h, new_w]) resized = images # BCHW format images = np.transpose(images, (0, 3, 1, 2)) # RGB to BGR images = images[:, ::-1, :, :] # to float and to 0-1 images = images / 255.0 # as continuous memory images = np.ascontiguousarray(images, dtype=np.float32) dark_frames = Image(images) return dark_frames, resized, images
def Detection(self, img): #print("test0") param = Image(img) #print(param) #print("test1") results = self.net.detect(param) #print("RESULTS: ", results) detect_list = [] #print("list: ", detect_list) # by hcw for cat, score, bounds in results: x, y, w, h = bounds cv2.rectangle(img, (int(x - w / 2), int(y - h / 2)), (int(x + w / 2), int(y + h / 2)), (255, 0, 0), thickness=2) cv2.putText(img, str(cat.decode("utf-8")), (int(x - w / 2), int(y + h / 4)), cv2.FONT_HERSHEY_COMPLEX, 1, (255, 255, 255)) detect_list.append(cat.decode()) print("test") cv2.imshow('dect', img) print("test2") cv2.waitKey(1) # hcw self.steer.Set_ObjectDetection(detect_list)
def feature_predict(self, imgpath): net = Detector( bytes("custom/yolov3-tiny.cfg", encoding="utf-8"), bytes("custom/yolov3-tiny_400.weights", encoding="utf-8"), 0, bytes("cfg/coco.data", encoding="utf-8")) multiplier_array = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1] print(multiplier_array) img = cv2.imread("static/img/" + imgpath, cv2.COLOR_BGR2RGB) img2 = Image(img) results = net.detect(img2, 0.4) if "Black Spots on Orange" in str(results): print("Black Spots found in input image, creating bias for class") multiplier_array[4] = 1.5 if "Top White Fringe" in str(results): print( "Top White Fringe found in input image, creating bias for class" ) multiplier_array[4] = 1.5 for cat, score, bounds in results: x, y, w, h = bounds cv2.rectangle(img, (int(x - w / 2), int(y - h / 2)), (int(x + w / 2), int(y + h / 2)), (255, 0, 0), thickness=2) cv2.putText(img, str(cat.decode("utf-8")), (int(x), int(y)), cv2.FONT_HERSHEY_COMPLEX, 1, (255, 255, 0)) cv2.imwrite('static/features/' + imgpath, img) return multiplier_array
def predict(self): """ Runs the neural network """ # Check if cached if self._results is None: # Run neural network self._results = self._net.detect(Image(self._image)) # Init lists self._ball_candidates = [] self._goalpost_candidates = [] # Go through results for out in self._results: # Get class id class_id = out[0] # Get confidence confidence = out[1] # Get candidate position and size x, y, w, h = out[2] x = x - int(w // 2) y = y - int(h // 2) # Create candidate c = Candidate(int(x), int(y), int(w), int(h), confidence) # Append candidate to the right list depending on the class if class_id == b"ball": self._ball_candidates.append(c) if class_id == b"goalpost": self._goalpost_candidates.append(c)
def predict(self, imgcv): from pydarknet import Image copy_image = imgcv.copy() formatted_dets = [] origin_h, origin_w, _ = imgcv.shape ratio_h = origin_h / self.in_h ratio_w = origin_w / self.in_w imgcv = cv2.resize(imgcv, (self.in_w, self.in_h)) dark_frame = Image(imgcv) results = self.net.detect(dark_frame) del dark_frame for cat, score, bounds in results: if cat == b'person' and score >= self.detect_threshold: x, y, w, h = bounds x, w = x * ratio_w, w * ratio_w y, h = y * ratio_h, h * ratio_h x1, y1 = max(0, int(x - w // 2)), max(0, int(y - h // 2)) x2, y2 = max(0, int(x + w // 2)), max(0, int(y + h // 2)) bbox_img = copy_image[y1:y2, x1:x2] detection = get_default_detection() detection['person_bbox'] = [x1, y1, x2, y2] detection['person_img'] = bbox_img detection['person_confidence'] = score formatted_dets.append(detection) return formatted_dets
def obj_detect(img): start_time = time.time() img_darknet = Image(img) results = net.detect(img_darknet) end_time = time.time() #print("Elapsed Time:",end_time-start_time) x, y, w, h = 0, 0, 0, 0 objString = "" for category, score_cat, bounds in results: cat = category.decode("utf-8") sco = str(int(score_cat * 100)) + '%' #print("A:", cat, sco) objString += cat + "(" + sco + ") " #print("B:", objString) x, y, w, h = bounds print("cat:{}, string:{}".format(cat, objString)) cv2.rectangle(img, (int(x - w / 2), int(y - h / 2)), (int(x + w / 2), int(y + h / 2)), (255, 0, 0)) cv2.putText(img, cat + '(' + sco + ')', (int(x), int(y)), cv2.FONT_HERSHEY_COMPLEX, 1, (255, 255, 0)) #if(catString != ""): #print("RETURN:", objString) return (img, objString)
def predict_yolo(net, img, n_rect=7, n_rect_search=200, yolo_thresh=0.1, nms=.25): img2 = Image(img) #detect(self, Image image, float thresh=.5, float hier_thresh=.5, float nms=.45) results = [] if net is None else net.detect_foreground( img2, thresh=yolo_thresh, nms=nms) #results = [] centers = [] scores = [] for score, bounds in results: x, y, w, h = bounds centers += [[x - w / 2, y - h / 2, x + w / 2, y + h / 2]] scores += [score] centers = np.asarray(centers, dtype=np.float32) scores = np.asarray(scores, dtype=np.float32) order = np.argsort(-scores)[:n_rect] centers = centers[order] scores = scores[order] if len(centers) == 0: # if no object detected return predict_selective_search(img, n_rect=n_rect, n_rect_search=n_rect_search) return centers, scores
def model_api(url): car_exists = False try: if 'file://' in url: with open(url.replace('file://', ''), 'rb') as f: image = np.asarray(bytearray(f.read()), dtype="uint8") elif 's3://' in url: import boto3 s3 = boto3.client('s3') bucket = url.split('/')[2] key = '/'.join(url.split('/')[3:]) obj = s3.get_object(Bucket=bucket, Key=key) image = np.asarray(bytearray(obj['Body'].read()), dtype="uint8") else: resp = urllib.request.urlopen(url) image = np.asarray(bytearray(resp.read()), dtype="uint8") img = cv2.imdecode(image, cv2.IMREAD_COLOR) img2 = Image(img) # r = net.classify(img2) results = net.detect(img2) response = [] for cat, score, bbox in results: response.append({ 'category': cat.decode(), 'proba': score, 'bbox': bbox }) print(response) return response except Exception as e: return 'unknown'
def camera_test(): net = Detector(bytes("cfg/yolov3.cfg", encoding="utf-8"), bytes("weights/yolov3.weights", encoding="utf-8"), 0, bytes("cfg/coco.data", encoding="utf-8")) print("Attempting to capture webcam") cap = cv2.VideoCapture(0) print("Got capture") while True: r, frame = cap.read() if r: start_time = time.time() # Only measure the time taken by YOLO and API Call overhead dark_frame = Image(frame) results = net.detect(dark_frame) del dark_frame end_time = time.time() print("Elapsed Time:", end_time - start_time) for cat, score, bounds in results: x, y, w, h = bounds cv2.rectangle(frame, (int(x - w / 2), int(y - h / 2)), (int(x + w / 2), int(y + h / 2)), (255, 0, 0)) cv2.putText(frame, str(cat.decode("utf-8")), (int(x), int(y)), cv2.FONT_HERSHEY_COMPLEX, 1, (255, 255, 0)) cv2.imshow("preview", frame) k = cv2.waitKey(1) if k == 0xFF & ord("q"): break
def detect(cfg, weights, coco_data, source_video): print('Detecting vehicles (This may take time)...') net = Detector(bytes(cfg, encoding="utf-8"), bytes(weights, encoding="utf-8"), 0, bytes(coco_data,encoding="utf-8")) detections = [] i = 0 cap = cv2.VideoCapture(source_video) while True: ret, frame = cap.read() if not ret: break img = Image(frame) results = net.detect(img) if i%500==0: print(i) i+=1 detections.append(results) pkl.dump(detections, open('detections.pkl', 'wb'))
def predict(self): """ Runs the neural network """ # Check if cached if self._face_candidates is None or not self._caching: # Run neural network results = self._net.detect(Image(self._image)) # Init lists self._face_candidates = [] # Go through results for out in results: # Get class id class_id = out[0] # Get confidence confidence = out[1] if confidence > self._confidence_threshold: # Get candidate position and size x, y, w, h = out[2] x = x - int(w // 2) y = y - int(h // 2) # Create candidate c = Candidate(int(x), int(y), int(w), int(h), confidence) # Append candidate to the right list depending on the class if class_id == b"face": self._face_candidates.append(c)
def predict(self): """ Runs the neural network """ # Check if cached if self._candidates is None or not self._caching: # Run neural network results = self._net.detect(Image(self._image)) # Init lists self._candidates = defaultdict(list) # Go through results for out in results: # Get class id class_id = out[0] # Get confidence confidence = out[1] if confidence > self._confidence_threshold: # Get candidate position and size x, y, w, h = out[2] x = x - int(w // 2) y = y - int(h // 2) # Create candidate c = Candidate(int(x), int(y), int(w), int(h), confidence) # Append candidate to the right list depending on the class assert class_id.decode() in self._class_names, \ f"Predicted class {class_id.decode()} not in {self._class_names}." self._candidates[class_id.decode()].append(c)
def detect(frame, net): clf = cc.load() mul = 1 if mul != 1: framez = cv2.resize(frame, (0, 0), fx=1 / mul, fy=1 / mul) else: framez = frame start_time = time.time() # Only measure the time taken by YOLO and API Call overhead dark_frame = Image(framez) results = net.detect(dark_frame) del dark_frame end_time = time.time() # print("Elapsed Time:", 1/(end_time-start_time)) cropped_image = [] for cat, score, bounds in results: x, y, w, h = bounds cv2.rectangle(framez, (int(x - w / 2), int(y - h / 2)), (int(x + w / 2), int(y + h / 2)), (255, 0, 0)) cropped_image.append(framez[int(x - w / 2):int(x + w / 2), int(y - h / 2):int(y + h / 2)]) color = cc.get_color(cropped_image[-1], clf) cv2.putText(framez, str(cat.decode("utf-8")), (int(x), int(y)), cv2.FONT_HERSHEY_COMPLEX, 1, (255, 255, 0)) results.append( (score, color, (int(x - w / 2), int(y - h / 2)), (int(x + w / 2), int(y + h / 2)))) #cv2.imshow("frame", framez) results.append(framez) #results = [(cat, score, (mul*(bounds[0]-bounds[2]/2),mul*(bounds[1]-bounds[3]/2),mul*(bounds[2]),mul*(bounds[3]))) for cat, score, bounds in results] return results
def render(self, display): if self.image is not None: array = np.frombuffer(self.image.raw_data, dtype=np.dtype("uint8")) array = np.reshape(array, (self.image.height, self.image.width, 4)) array = array[:, :, :3] # draw labels img = cv2.resize(array, None, fx=1, fy=1) height, width, channels = img.shape img_darknet = Image(img) outputs = net.detect(img_darknet) boxes = [] confs = [] class_ids = [] for _ in outputs: boxes.append([int(i) for i in _[2]]) confs.append(_[1]) class_ids.append(classes.index(_[0].decode(encoding='utf-8'))) array = draw_labels(boxes, confs, colors, class_ids, classes, img) array = array[:, :, ::-1] surface = pygame.surfarray.make_surface(array.swapaxes(0, 1)) display.blit(surface, (0, 0))
def yolo_plants(img): net = Detector( bytes("cfg.taichun/yolov3-tiny.cfg", encoding="utf-8"), bytes("cfg.taichun/weights/yolov3-tiny_3600.weights", encoding="utf-8"), 0, bytes("cfg.taichun/obj.data", encoding="utf-8")) img2 = Image(img) results = net.detect(img2) for cat, score, bounds in results: cat = cat.decode("utf-8") if (cat == "Pteris_cretica"): boundcolor = (0, 238, 252) elif (cat == "Echeveria_Minibelle"): boundcolor = (227, 252, 2) elif (cat == "Crassula_capitella"): boundcolor = (249, 77, 190) x, y, w, h = bounds cv2.rectangle(img, (int(x - w / 2), int(y - h / 2)), (int(x + w / 2), int(y + h / 2)), (255, 0, 0), thickness=2) boundbox = cv2.imread("images/" + cat + ".jpg") print("read:", "images/" + cat + ".jpg") print(y, boundbox.shape[0], x, boundbox.shape[1]) #img[ int(y-h/2):int(y-h/2)+boundbox.shape[0], int(x-w/2):int(x-w/2)+boundbox.shape[1]] = boundbox img[int(y):int(y + boundbox.shape[0]), int(x):int(x + boundbox.shape[1])] = boundbox return img
def gen(): # Optional statement to configure preferred GPU. Available only in GPU version. # pydarknet.set_cuda_device(0) while True: frame = video_capture.read() frame = cv2.resize(frame, (0, 0), fx=0.5, fy=0.5) start_time = time.time() # Only measure the time taken by YOLO and API Call overhead dark_frame = Image(frame) results = net.detect(dark_frame) del dark_frame end_time = time.time() print("Elapsed Time :", end_time - start_time) for cat, score, bounds in results: x, y, w, h = bounds cv2.rectangle(frame, (int(x - w / 2), int(y - h / 2)), (int(x + w / 2), int(y + h / 2)), (255, 0, 0)) cv2.putText(frame, str(cat.decode("utf-8")), (int(x), int(y)), cv2.FONT_HERSHEY_COMPLEX, 1, (255, 255, 0)) ret, jpeg = cv2.imencode('.jpeg', frame) jpeg = jpeg.tobytes() # jpeg = jpeg.tobytes() yield (b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' + jpeg + b'\r\n\r\n')
def main(input_img): ''' Run YOLO on an image ''' if (input_img): image = stringToImage(input_img[input_img.find(",") + 1:]) image = toRGB(image) height, width, channels = image.shape img = Image(image) results = net.detect(img) parsed_results = [] for cat, score, bounds in results: x, y, w, h = bounds parsed_results.append({ "cat": cat.decode("utf-8"), "score": score, "bounds": [(x - w / 2) / width, (y - h / 2) / height, w / width, h / height] }) return parsed_results else: # Test with a sample image img = cv2.imread(os.path.join("data", "dog.jpg")) height, width, channels = img.shape img2 = Image(img) results = net.detect(img2) parsed_results = [] for cat, score, bounds in results: x, y, w, h = bounds parsed_results.append({ "cat": cat.decode("utf-8"), "score": score, "bounds": [(x - w / 2) / width, (y - h / 2) / height, w / width, h / height] }) return parsed_results
def classify(): # # access control # args = request.args client_id = None try: client_id = args['AUTH'] except KeyError: pass if client_id not in accessControl.permitted: return jsonify(dict(STATUS="FORBIDDEN", )) access_token = None try: access_token = args['TOKEN'] except: pass if ((access_token == None) or (access_token != accessControl.token_current)): time.sleep(random.uniform(0, 3)) return jsonify( dict( AUTH=client_id, STATUS=accessControl.status, ERROR= "Wrong access token. Use /accessControl.status to get the token." )) accessControl.token_current = None # # acess control stop # try: fileBytes = request.files['filedata'] fileName = request.form['name'] # in_memory_file = io.BytesIO() fileBytes.save(in_memory_file) fileData = np.frombuffer(in_memory_file.getvalue(), dtype=np.uint8) nparr = np.frombuffer(fileData, np.uint8) # decode image img = cv2.imdecode(nparr, cv2.IMREAD_COLOR) # dark_frame = Image(img) results = net.detect(dark_frame) del dark_frame # encode response using jsonpickle response = str(results) # return response except Exception as ex: response = "Exception:" + str(ex) + ' at ' + getActiveLine() return response
def Detection(self, img): cv2.imshow(Image(img)) results = self.net.detect(Image(img)) detect_list = [] print(results) for cat, score, bounds in results: x, y, w, h = bounds cv2.rectangle(img, (int(x - w / 2), int(y - h / 2)), (int(x + w / 2), int(y + h / 2)), (255, 0, 0), thickness=2) cv2.putText(img, str(cat.decode("utf-8")), (int(x - w / 2), int(y + h / 4)), cv2.FONT_HERSHEY_COMPLEX, 1, (255, 255, 255)) detect_list.append(cat.decode()) cv2.imshow('dect', img) self.object = detect_list
def yoloPython(img): img2 = Image(img) results = net.detect(img2) for cat, score, bounds in results: cat = cat.decode("utf-8") print(cat) if (cat == "0_pepper_flower"): boundcolor = (255, 255, 255) labelName = "flower" elif (cat == "1_pepper_young"): boundcolor = (193, 161, 31) labelName = "young" elif (cat == "2_pepper_matured"): boundcolor = (12, 255, 240) labelName = "pepper" else: boundcolor = (255, 255, 255) labelName = "unknow" x, y, w, h = bounds cv2.rectangle(img, (int(x - w / 2), int(y - h / 2)), (int(x + w / 2), int(y + h / 2)), boundcolor, thickness=3) boundbox = cv2.imread("cfg.pepper/images/" + cat + ".jpg") print("boundbox:", boundbox.shape) start_x = int(x - w / 2) start_y = int(y - h / 2) - boundbox.shape[0] ''' if(start_x<0): start_x = 0 if(start_y<0): start_y = 0 end_x=start_x+boundbox.shape[1] end_y=start_y+boundbox.shape[0] if(end_x>img.shape[1]): end_x = img.shape[1] if(end_y>img.shape[0]): end_y = img.shape[0] ''' end_x = start_x + boundbox.shape[1] end_y = start_y + boundbox.shape[0] print( "(end_x-start_x)={}, (end_y-start_y)={}, img.shape[1]={}, img.shape[0]={}" .format((end_x - start_x), (end_y - start_y), boundbox.shape[1], boundbox.shape[0])) try: img[start_y:end_y, start_x:end_x] = boundbox print("read:", "images/" + cat + ".jpg") except: print("add text: ", labelName) cv2.putText(img, labelName, (int(x), int(y)), cv2.FONT_HERSHEY_COMPLEX, 1.6, boundcolor, 2) return img
def yoloDetect(img): # net = Detector(bytes("cfg/densenet201.cfg", encoding="utf-8"), bytes("densenet201.weights", encoding="utf-8"), 0, bytes("cfg/imagenet1k.data",encoding="utf-8")) net = Detector(bytes("/home/sfc_kamata/work/SFC_kamata/darknet_update/cfg/jingle/test.cfg", encoding="utf-8"), bytes("/home/sfc_kamata/work/SFC_kamata/darknet_update/cfg/jingle/backup/1208_1/yolo-obj_class4_20000.weights", encoding="utf-8"), 0, bytes("/home/sfc_kamata/work/SFC_kamata/darknet_update/cfg/jingle/jingle.data",encoding="utf-8")) #img = cv2.imread(os.path.join(os.environ["DARKNET_HOME"],"data/dog.jpg")) #img = cv2.imread("/home/sfc_kamata/work/SFC_kamata/darknet_update/cfg/jingle/img/1.jpg") img2 = Image(img) # r = net.classify(img2) results = net.detect(img2) #writeBoundingBox(results) return results
def run(self, frame): dark_frame = Image(frame) results = self.net.detect(dark_frame) # print(serializedResult) # cv2.imshow('frame',frame) # if cv2.waitKey(1) & 0xFF == ord('q'): # break #del dark_frame return results
def LabelVideoCam(): #average_time = 0 cap = cv2.VideoCapture(0) cap.set(cv2.CAP_PROP_FPS, 20) fourcc = cv2.VideoWriter_fourcc(*'XVID') out = cv2.VideoWriter('output.mp4', fourcc, 20.0, (640, 480)) Objects = open('DetectedObjects/objects.tsv', 'w') Objects.write('Frames' + '\t' + 'Objects' + '\t' + 'Score' + '\t' + 'X0' + '\t' + 'Y0' + '\t' + 'X' + '\t' + 'Y\n') i = 1 while True: r, frame = cap.read() if r: #start_time = time.time() # Only measure the time taken by YOLO and API Call overhead dark_frame = Image(frame) results = net.detect(dark_frame) del dark_frame #end_time = time.time() #average_time = average_time * 0.8 + (end_time-start_time) * 0.2 #print("Total Time:", end_time-start_time, ":", average_time) for cat, score, bounds in results: x, y, w, h = bounds cv2.rectangle(frame, (int(x - w / 2), int(y - h / 2)), (int(x + w / 2), int(y + h / 2)), (255, 0, 0)) cv2.putText(frame, str(cat.decode("utf-8")), (int(x), int(y)), cv2.FONT_HERSHEY_COMPLEX, 1, (255, 255, 0)) Objects.write( str(i) + '\t' + str(cat.decode("utf-8")) + '\t' + str(score) + '\t' + str(int(x - w / 2)) + '\t' + str(int(y - h / 2)) + '\t' + str(int(x + w / 2)) + '\t' + str(int(y + h / 2)) + '\n') cv2.imshow("preview", frame) out.write(frame) i += 1 k = cv2.waitKey(1) if k == 0xFF & ord("q"): break cap.release() out.release() Objects.close() cv2.destroyAllWindows()
def recognize(file): #Read img #img = cv2.imread(os.path.join(os.environ["DARKNET_HOME"], filename)) img = cv2.imread(file) dark_img = Image(img) print("Filename: ", file) #create darknet detector net = Detector(bytes("cfg/yolofinal.cfg", encoding="utf-8"), bytes("weights/yolov3.weights", encoding="utf-8"), 0, bytes("cfg/data.data", encoding="utf-8")) res = [] start_time = time.time() #Detect! can be given threshold parameters - see top of file :) #Has a standard threshold value of .5 results = net.detect(dark_img) end_time = time.time() if len(results) == 0: print("No results") else: print("There are Results") # print(results) print("Elapsed Time:", end_time - start_time) for cat, score, bounds in results: x, y, w, h = bounds #bytecode-decode to string cat_str = cat.decode("utf-8") #Format JSON json_obj = { "x": x, "y": y, "w": w, "h": h, "cat": cat_str, "score": score } #Append JSON to list res.append(json_obj) cv2.rectangle(img, (int(x - w / 2), int(y - h / 2)), (int(x + w / 2), int(y + h / 2)), (255, 0, 0), thickness=2) cv2.putText(img, str(cat.decode("utf-8")), (int(x), int(y)), cv2.FONT_HERSHEY_DUPLEX, 4, (0, 0, 255), thickness=2) print(res) cv2.imwrite(os.path.join("output", "outputpic.png"), img) return json.dumps(res)
def get_vacant_spots_from_image(image): net = Detector(bytes("cfg/yolov3.cfg", encoding="utf-8"), bytes("weights/yolov3.weights", encoding="utf-8"), 0, bytes("cfg/coco.data", encoding="utf-8")) img = Image(image) results = net.detect(img) print(results) for cat, score, bounds in results: x, y, w, h = bounds return ""
def predict(): # initialize the data dictionary that will be returned from the # view data = {"success": False} # ensure an image was properly uploaded to our endpoint if flask.request.method == "POST": decoded = flask.request.data.decode("utf-8") print("decoding ready") received = json.loads(decoded) if received.get("image"): # read the image in PIL format start_img_proc = time() image = b64decode(received["image"]) image = pil.open(io.BytesIO(image)) cv2_image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR) darknet_img = Image(cv2_image) img_proc = time() - start_img_proc print("Img proc time:") print(img_proc) model_start = time() results = model.detect(darknet_img) model_proc = time() - model_start print("model proc time") print(model_proc) for cat, score, bounds in results: x, y, w, h = bounds cv2.rectangle(cv2_image, (int(x - w / 2), int(y - h / 2)), (int(x + w / 2), int(y + h / 2)), (255, 0, 0), thickness=2) cv2.putText(cv2_image, str(cat.decode("utf-8")), (int(x), int(y)), cv2.FONT_HERSHEY_DUPLEX, 4, (0, 0, 255), thickness=2) buffered = BytesIO() image.save(buffered, format="JPEG") img_bytes = b64encode(buffered.getvalue()) img_str = img_bytes.decode('utf-8') return flask.jsonify({"result": img_str}) return flask.jsonify({ "failure": "Not valid input", "received": str(flask.request.data) })
def detect_objects(self, img): # Digestable by the darknet yolo_image = Image(img) results = self.net.detect(yolo_image) # Prepare the response objects = [] for it, result in enumerate(results): an_object = yolo2filestack(result) objects.append(an_object) scores = {'objects': objects} return scores
def process(record): values = json.loads(record[1]) data = values['data'] videoid = values['video_id'] metadata = values['metadata'] frameNum = metadata['id'] nparr = np.reshape( np.frombuffer(base64.b64decode(data.encode('utf8')), np.uint8), (metadata['rows'], metadata['cols'], metadata['channels'])) start_time = time.time() frame = Image(nparr) net = Detector(bytes("yolov3.cfg", encoding="utf-8"), bytes("yolov3.weights", encoding="utf-8"), 0, bytes("coco.data", encoding="utf-8")) results = net.detect(frame) del frame end_time = time.time() # print("Elapsed Time:",end_time-start_time) categories = [] for cat, score, bounds in results: x, y, w, h = bounds categories.append(str(cat.decode("utf-8"))) cv2.rectangle(nparr, (int(x - w / 2), int(y - h / 2)), (int(x + w / 2), int(y + h / 2)), (255, 0, 0)) cv2.putText(nparr, str(cat.decode("utf-8")), (int(x), int(y)), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 0, 255)) #cv2.imwrite('yolo-output/'+str(frameNum)+'.jpg', nparr) # print('process done') # save to hbase yoloframe = 'cts:%s' % frameNum #metadata1 = 'mtd:%s' % frameNum labels = 'lbs:%s' % frameNum # metadata = json.dumps(metadata).encode() categ = json.dumps(categories).encode() key_vals = { yoloframe.encode(): base64.b64encode(nparr), labels.encode(): categ } key_vals[b'mtd:rows'] = str(metadata['rows']).encode('utf8') key_vals[b'mtd:cols'] = str(metadata['cols']).encode('utf8') key_vals[b'mtd:channels'] = str(metadata['channels']).encode('utf8') key_vals[b'mtd:fr'] = str(metadata['frame_rate']).encode('utf8') key_vals[b'mtd:dur'] = str(metadata['duration']).encode('utf8') key_vals[b'mtd:fc'] = str(metadata['total_frames']).encode('utf8') return (videoid.encode(), key_vals)
def get_bounding_boxes_gpu(img): from pydarknet import Image img_darknet = Image(img) results = net.detect(img_darknet) _bounding_boxes, _classes, _confidences = [], [], [] for cat, score, bounds in results: _class = str(cat.decode('utf-8')) if score > CONG_THRESHOLD and _class in CLASSES_OF_INTEREST: _bounding_boxes.append(bounds) _classes.append(_class) _confidences.append(score) return _bounding_boxes, _classes, _confidences