示例#1
0
 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)
示例#2
0
class YoloHandler():
    def __init__(self, config, model_path):
        weightpath = os.path.join(model_path, "yolo_weights.weights")
        configpath = os.path.join(model_path, "config.cfg")
        datapath = os.path.join("/tmp/obj.data")
        namepath = os.path.join(model_path, "obj.names")

        self._generate_dummy_obj_data_file(namepath)

        self.config = config

        self.net = Detector(bytes(configpath, encoding="utf-8"),
                            bytes(weightpath, encoding="utf-8"), 0.5,
                            bytes(datapath, encoding="utf-8"))
        self.classes = ["ball", "goalpost"]
        self.image = None
        self.results = None
        self.goalpost_candidates = None
        self.ball_candidates = None

    def _generate_dummy_obj_data_file(self, obj_name_path):
        obj_data = "classes = 2\nnames = " + obj_name_path
        with open('/tmp/obj.data', 'w') as f:
            f.write(obj_data)

    def set_image(self, img):
        if np.array_equal(img, self.image):
            return
        self.image = img
        self.results = None
        self.image = img
        self.goalpost_candidates = None
        self.ball_candidates = None

    def predict(self):
        if self.results is None:
            self.results = self.net.detect(Image(self.image))
            class_ids = []
            confidences = []
            boxes = []
            self.ball_candidates = []
            self.goalpost_candidates = []
            for out in self.results:
                print("********{}********".format(out[0]))
                class_id = out[0]
                confidence = out[1]
                x, y, w, h = out[2]
                x = x - int(w // 2)
                y = y - int(h // 2)
                c = Candidate(int(x), int(y), int(w), int(h))
                c.rating = confidence
                class_id = class_id
                if class_id == b"ball":
                    self.ball_candidates.append(c)
                if class_id == b"goalpost":
                    self.goalpost_candidates.append(c)

    def get_candidates(self):
        self.predict()
        return [self.ball_candidates, self.goalpost_candidates]
示例#3
0
def test_image():

    assert r.status_code == 200
    img = PIL.Image.open('/home/nvidia/Desktop/download.jpg')

    img = np.array(img)
    img = img[:, :, ::-1]  # RGB to BGR

    cfg = bytes(
        "/home/nvidia/Documents/yolov3-tiny-tennis/yolov3-tiny-tennis.cfg",
        encoding="utf-8")
    weights = bytes(
        "/home/nvidia/Documents/yolov3-tiny-tennis/yolov3-tiny-tennis_last.weights",
        encoding="utf-8")
    data = bytes("/home/nvidia/Documents/darknet/data/obj.data",
                 encoding="utf-8")

    net = Detector(cfg, weights, 0, data)

    img2 = Image(img)

    results = net.detect(img2)
    print(results)

    results_labels = [x[0].decode("utf-8") for x in results]
示例#4
0
    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
示例#5
0
class Object_Detection(object):
    def __init__(self, steer):

        self.steer = steer

        # Detection 및 Classification에 사용할 모델 구조(cfg), 학습한 가중치 결과(weights), label 분류 파일(data) 파일을
        # Detector의 매개변수로 지정한다.
        self.net = Detector(bytes("./yolo/cfg/yolo-obj.cfg", encoding="utf-8"),
                            bytes("./yolo/cfg/yolo-obj_400.weights", encoding="utf-8"),
                            0,
                            bytes("./yolo/obj.data", encoding="utf-8"))

    def detection(self, img):
        results = self.net.detect(Image(img))       
       
        detect_list = []

        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.steer.Set_ObjectDetection(detect_list)
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'))
class ObjectDetection:

    def __init__(self):

        root = '/app'

        configPath  = root + '/brain/yolov3.cfg'
        weightsPath = root + '/brain/yolov3.weights'
        classesPath = root + '/brain/coco.data'

        self.net = Detector(bytes(configPath, encoding="utf-8"), 
                        bytes(weightsPath, encoding="utf-8"), 0,
                        bytes(classesPath, encoding="utf-8"))


    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
示例#8
0
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
示例#9
0
class Object_Detection():
    def __init__(self, steer):
        # 저장된 YOLO 모델을 호출함
        self.steer = steer
        self.net = Detector(
            bytes("YOLOv3/cfg/yolo-obj.cfg", encoding="utf-8"),
            bytes("YOLOv3/cfg/yolo-obj_400.weights", encoding="utf-8"), 0,
            bytes("YOLOv3/cfg/obj.data", encoding="utf-8"))

    # 이미지를 입력받아 detection과 classification 결과를 리턴함
    def Detection(self, img):

        results = self.net.detect(Image(img))

        detect_list = []

        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.steer.Set_ObjectDetection(detect_list)
示例#10
0
def test_image():
    r = requests.get(
        "https://raw.githubusercontent.com/madhawav/darknet/master/data/dog.jpg"
    )
    assert r.status_code == 200
    img = PIL.Image.open(BytesIO(r.content))

    img = np.array(img)
    img = img[:, :, ::-1]  # RGB to BGR

    net = Detector(bytes("cfg/yolov3.cfg", encoding="utf-8"),
                   bytes("weights/yolov3.weights", encoding="utf-8"), 0,
                   bytes("cfg/coco.data", encoding="utf-8"))

    img2 = Image(img)

    results = net.detect(img2)
    print(results)
    results_labels = [x[0].decode("utf-8") for x in results]
    print(results_labels)

    assert "bicycle" in results_labels
    assert "dog" in results_labels
    assert "truck" in results_labels
    assert len(results_labels) == 3
示例#11
0
class Object_Detection():
    def __init__(self, steer):

        self.steer = steer
        '''
        self.net = Detector(bytes("YOLOv3/cfg/Noruway.cfg", encoding="utf-8"),
                   bytes("YOLOv3/cfg/Noruway_200.weights", encoding="utf-8"),
                   0, 
                   bytes("YOLOv3/cfg/Noruway.data", encoding="utf-8"))
        '''
        print("ENTERED INIT")
        self.net = Detector(
            bytes("YOLOv3/cfg/yolov2.cfg", encoding="utf-8"),
            bytes("YOLOv3/cfg/yolov2.weights", encoding="utf-8"), 0,
            bytes("YOLOv3/cfg/coco.data", encoding="utf-8"))
        print("Exiting INIT")

        # with open("YOLOv3/data/coco.name", 'r') as f:
        #     self.classes = [line.strip() for line in f.readlines()]
        #
        # self.COLORS = np.random.uniform(0, 255, size=(len(self.classes), 3))

    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

            # print('x type :', type(x))
            # print('y type :', type(y))

            #print(bounds)
            # if np.isnan(x):
            #     print('nan')
            # else:
            color = np.random.uniform(0, 255, size=3)
            cv2.rectangle(img, (int(x - w / 2), int(y - h / 2)),
                          (int(x + w / 2), int(y + h / 2)),
                          color,
                          thickness=2)
            cv2.putText(img, str(cat.decode("utf-8")),
                        (int(x - w / 2), int(y - h / 2)),
                        cv2.FONT_HERSHEY_SIMPLEX, 0.5, color)
            detect_list.append(cat.decode())

        #print(detect_list)
        #print("test")
        cv2.imshow('dect', img)
        #print("test2")
        cv2.waitKey(1)  # hcw

        self.steer.Set_ObjectDetection(detect_list)
示例#12
0
class DarknetYoloV3Analyze:

    def __init__(self):
        self.net = Detector(bytes("cfg/yolov3.cfg", encoding="utf-8"), bytes("wgt/yolov3.weights", encoding="utf-8"), 0,
                       bytes("cfg/coco.data", encoding="utf-8"))

    def analyze(self, filename: str, folder: str) -> AnnotationModel:
        path = folder+'/'+filename
        frame = cv2.imread(path)
        dark_frame = Image(frame)
        results = self.net.detect(dark_frame)
        ah, aw, ad = frame.shape
        annotation_size = AnnotationSize(aw, ah, ad)
        objects = []
        for cat, score, bounds in results:
            x, y, w, h = bounds
            # print("output: ", x, " ", y, " ", w, " ", h, "tot: ", aw, " ", ah)
            bndbox = {
                'xmin': x-w/2,
                'xmax': x+w/2,
                'ymin': y-h/2,
                'ymax': y+h/2,
            }
            if cat.decode('utf-8') == 'person':
                objects += [AnnotationObjectModel(str('0'), bndbox)]
        annotation_model = AnnotationModel(filename, folder, folder+'/'+filename,
                                           annotation_size, objects)
        return annotation_model
示例#13
0
class YOLO:
    # Optional statement to configure preferred GPU. Available only in GPU version.
    # pydarknet.set_cuda_device(0)

    def __init__(self):
        self.net = Detector(
            bytes("cfg/danyolo.cfg", encoding="utf-8"),
            bytes("weights/danyolo_900.weights", encoding="utf-8"), 0,
            bytes("cfg/obj.names", encoding="utf-8"))

        # Only measure the time taken by YOLO and API Call overhead
    def detect(self, frame):

        dark_frame = Image(frame)
        results = self.net.detect(dark_frame)
        del dark_frame
        bboxes = []
        cats = []
        scores = []
        for cat, score, bounds in results:
            x, y, w, h = bounds
            bboxes.append((int(x - w / 2), int(y - h / 2)),
                          (int(x + w / 2), int(y + h / 2)))
            cats.append(cat)
            scores.append(score)

        return bboxes, cats, scores
示例#14
0
class YoloV3:
  def __init__(self, config):
    if 'weights' in config:
      self.weights = bytes(config['weights'], encoding='utf-8')
    else:
      raise KeyError('weights key is missing')
    if 'data' in config:
      self.data = bytes(config['data'], encoding='utf-8')
    else:
      raise KeyError('data key is missing')
    if 'cfg' in config:
      self.cfg = bytes(config['cfg'], encoding='utf-8')
    else:
      raise KeyError('cfg key is missing')
    print(self.data, self.cfg, self.weights)
    # Init the network
    self.net = Detector(self.cfg, self.weights, 0, self.data)

  def predict(self, frame):
    dark_frame = Image(frame)
    results = self.net.detect(dark_frame)
    del dark_frame

    return results
  
  def segment(self, frame, classes=[]):
    predictions = self.predict(frame)
    for prediction in predictions:
      classLabel, confidence, (x, y, w, h) = prediction
      x1, x2, y1, y2 = int(x-w/2), int(x+w/2), int(y-h/2), int(y+h/2) 
      segment = frame[y1: y2,x1: x2,:]
      yield classLabel, confidence, segment
示例#15
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
示例#16
0
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
示例#17
0
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)
示例#18
0
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 ""
示例#19
0
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)
示例#20
0
class Object_Detection():

    def __init__(self, steer):

        self.steer = steer
        '''
        self.net = Detector(bytes("YOLOv3/cfg/Noruway.cfg", encoding="utf-8"),
                   bytes("YOLOv3/cfg/Noruway_200.weights", encoding="utf-8"),
                   0, 
                   bytes("YOLOv3/cfg/Noruway.data", encoding="utf-8"))
        '''
        print("ENTERED INIT")
        self.net = Detector(bytes("YOLOv3/cfg/yolov1.cfg", encoding="utf-8"),
                   bytes("YOLOv3/cfg/yolo.weights", encoding="utf-8"),
                   0,
                   bytes("YOLOv3/cfg/coco.data", encoding="utf-8"))
        print("Exiting INIT")


    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)
示例#21
0
def yolo_insects(img):
    net = Detector(
        bytes("cfg.insects/yolov3-tiny.cfg", encoding="utf-8"),
        bytes("cfg.insects/weights/yolov3-tiny_95000.weights",
              encoding="utf-8"), 0,
        bytes("cfg.insects/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 == "0_ladybug"):
            boundcolor = (4, 5, 250)
        elif (cat == "1_Camellia"):
            boundcolor = (215, 158, 2)
        elif (cat == "2_Pieridae"):
            boundcolor = (57, 182, 6)
        elif (cat == "3_Lindinger"):
            boundcolor = (5, 70, 111)
        elif (cat == "4_Papilio_1_4"):
            boundcolor = (6, 148, 195)
        elif (cat == "5_Papilio_5"):
            boundcolor = (6, 148, 195)
        elif (cat == "6_ant"):
            boundcolor = (249, 7, 132)

        x, y, w, h = bounds
        print(x, y, w, h)

        xx = int(round(x, 0))
        yy = int(round(y, 0))
        ww = int(round(w, 0))
        hh = int(round(h, 0))
        cv2.rectangle(img, (int(xx - (ww / 2)), int(yy - (hh / 2))),
                      (int(xx + (ww / 2)), int(yy + (hh / 2))), (255, 0, 0),
                      thickness=2)

        boundbox = cv2.imread("images/" + cat + ".jpg")
        print("read:", "images/" + cat + ".jpg")
        print(boundbox.shape)
        print(yy, yy + boundbox.shape[0], xx, xx + 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[yy:yy + boundbox.shape[0], xx:xx + boundbox.shape[1]] = boundbox

    return img
def ObjectDetection():
    net = Detector(bytes(cfg_path, encoding="utf-8"),
                   bytes(weights_path, encoding="utf-8"), 0,
                   bytes(dataFile_path, encoding="utf-8"))
    #net = Detector(bytes("HexaphobiaV3_model/hex-testV1.cfg", encoding="utf-8"), bytes("HexaphobiaV3_model/yolov3-trainv1_9000.weights", encoding="utf-8"), 0, bytes("HexaphobiaV3_model/obj.data", encoding="utf-8"))
    pipeline, depth_scale = initCameraStreamRS()
    #Align --
    align_to = rs.stream.color
    align = rs.align(align_to)

    try:
        while True:
            frames = pipeline.wait_for_frames()
            aligned_frames = align.process(frames)
            color_frame = aligned_frames.get_color_frame()
            depth_frame = aligned_frames.get_depth_frame()
            depth_intrin = depth_frame.profile.as_video_stream_profile(
            ).intrinsics
            color_image = np.asanyarray(color_frame.get_data())
            img_darknet = Image(color_image)
            img = color_image
            results = net.detect(img_darknet)
            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))
                #d = rs.rs2_deproject_pixel_to_point(depth_intrin, [x,y], depth_scale)
                x1 = int(x)
                y1 = int(y)
                depthPoint = getmeanROI(depth_frame, h, w, x1, y1)
                #depthPoint = depth_frame.get_distance(x1, y1)
                print("test->", depthPoint)
                d = rs.rs2_deproject_pixel_to_point(depth_intrin, [x, y],
                                                    depthPoint)
                print("VectorD: ", d)
                dis = np.linalg.norm(d)
                print("Distance in meters:", dis)
                print("Detected: ", str(cat.decode("utf-8")), " Score:", score)
                LogFile(str(cat.decode("utf-8")), str(d), str(dis))
            cv2.imshow("output", img)
            cv2.waitKey(1)
    except KeyboardInterrupt:
        print("Detection terminated!!")
        pipeline.stop()
示例#23
0
def evaluateSet(setpath, filepath, filename):
    imagelist = readSet(setpath)
    net = Detector(bytes(cfgpath, encoding="utf-8"),
                   bytes(weightpath, encoding="utf-8"), 0,
                   bytes(objpath, encoding="utf-8"))
    rows = []
    counter = 0
    for i in imagelist:
        counter += 1
        img = cv2.imread(i)
        img_darknet = Image(img)
        results = net.detect(img_darknet)
        for cat, score, bounds in results:
            rows.append([i, cat, score, bounds])
    with open(filepath + filename, "a") as myfile:
        wr = csv.writer(myfile)
        for r in rows:
            wr.writerow(r)
示例#24
0
class design_fire_cv():

    #Load the darknet model
    def load_darknet(self):
        self.net = Detector(bytes(YOLOV3_PATH, encoding=ENCODING),\
                            bytes(WEIGHTS_PATH, encoding=ENCODING),0,\
                            bytes(COCO_DATA_PATH, encoding=ENCODING),\
                                )
    
    #Test an image
    def test_img_from_path(self,img_path):
        img = PIL.Image.open(img_path)
        img = np.array(img)
        img = img[:,:,::-1]
        img2 = Image(img)
        return self.test_img(img2)

    def test_img(self,img):
        results = self.net.detect(img)
        return results
示例#25
0
文件: darknet.py 项目: rstyczynski/Fn
def handler(ctx, data=None, loop=None):
    res = ""
    try:
        if data and len(data) > 0:
            #
            # 1. decode byte stream
            #
            nparr = np.frombuffer(data, np.uint8)
            #
            # 3. convert image
            #
            img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
            #
            net = Detector(bytes("cfg/yolov3.cfg", encoding="utf-8"),
                           bytes("weights/yolov3.weights", encoding="utf-8"),
                           0, bytes("cfg/coco.data", encoding="utf-8"))
            #
            dark_frame = Image(img)
            results = net.detect(dark_frame)
            del dark_frame
            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))
                cv2.putText(img, str(cat.decode("utf-8")), (int(x), int(y)),
                            cv2.FONT_HERSHEY_COMPLEX, 1, (255, 255, 0))
            #
            # 4. write response to byte array
            #
            retval, buf = cv2.imencode('.jpg', img)
            #
            # 5 Convert buffer to bytes for row response
            res = buf.tobytes()
    except Exception as ex:
        res = "Exception:" + str(ex) + ' at ' + getActiveLine()
        return res
    return response.RawResponse(ctx,
                                response_data=res,
                                headers={
                                    "Content-Type": "application/octet-stream",
                                })
示例#26
0
文件: yolo.py 项目: roszcz/yolo-api
class YOLO(object):
    def __init__(self):
        # https://pjreddie.com/darknet/yolo/
        self.net = Detector(bytes("yolo/yolov3.cfg", encoding="utf-8"),
                            bytes("yolo/yolov3.weights", encoding="utf-8"), 0,
                            bytes("yolo/coco.data", encoding="utf-8"))

    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
示例#27
0
def main(width=640, height=360, k=False):
    last_detected = datetime(1990, 1, 1)
    if k:
        cap = VideoCaptureAsync(0)
    else:
        cap = cv2.VideoCapture(0)
    cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
    cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
    if k:
        cap.start()
    t0 = time.time()
    i = 0
    net = Detector(bytes("cfg/yolo-lite.cfg", encoding="utf-8"),
                   bytes("moirai.weights", encoding="utf-8"), 0,
                   bytes("obj.data", encoding="utf-8"))
    while True:
        r, frame = cap.read()
        if r:
            dark_frame = Image(frame)
            results = net.detect(dark_frame)
            del dark_frame

            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, 255))
            if len(results) > 0:
                if datetime.now() > last_detected + timedelta(seconds=6):
                    last_detected = datetime.now()
                    prob = results[0][1]
                    requests.post('http://192.168.6.219:8080/area/alert',
                                  data={
                                      "cam_id": 1,
                                      "prob": prob
                                  })
        cv2.imshow('Frame', frame)
        cv2.waitKey(1) & 0xFF
    if k:
        cap.stop()
    cv2.destroyAllWindows()
示例#28
0
class Object_Detection():
    def __init__(self):
        self.net = Detector(
            bytes("cfg/yolo-obj.cfg", encoding="utf-8"),
            bytes("cfg/yolo-obj_400.weights", encoding="utf-8"), 0,
            bytes("cfg/obj.data", encoding="utf-8"))

    def Detection(self, img):
        img2 = Image(img)
        results = self.net.detect(img2)
        # print(results)
        # 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 - 1), int(y - h / 2 - 1)),
                        cv2.FONT_HERSHEY_COMPLEX, 1, (255, 255, 0))
            print(cat)
        return img
示例#29
0
def test_image():
    img = PIL.Image.open(
        BytesIO('darknet_cfg/test_images/test-f17-09-345.jpg'))

    img = np.array(img)
    img = img[:, :, ::-1]  # RGB to BGR

    net = Detector(
        bytes("darknet_cfg/yolov3-tiny.cfg", encoding="utf-8"),
        bytes("darknet_cfg/yolov3-tiny_10000.weights", encoding="utf-8"), 0,
        bytes("darknet_cfg/voc.names", encoding="utf-8"))

    img2 = Image(img)

    results = net.detect(img2)

    results_labels = [x[0].decode("utf-8") for x in results]

    assert "bicycle" in results_labels
    assert "dog" in results_labels
    assert "truck" in results_labels
    assert len(results_labels) == 3
示例#30
0
def darknet(
    proces_no,
    yolov3_cfg,
    yolov_weights,
    cat_encoding,
    obj_data,
):

    net = Detector(
        bytes(yolov3_cfg, encoding=cat_encoding),
        bytes(yolov_weights, encoding=cat_encoding),
        0,
        bytes(obj_data, encoding=cat_encoding),
    )
    path_to_picture = "/mnt/ramdisk/" + str(proces_no) + "/"
    while True:
        if check_if_image_complete(proces_no, path_to_picture, "frame.jpg"):
            read_frame = cv2.imread(path_to_picture + "/frame.jpg")
            dark_frame = Image(read_frame)
            results = net.detect(dark_frame, thresh=detection_treshold)
            print("Results:", proces_no, results)
            del dark_frame
sh  download_models.sh
wget -C http://absfreepic.com/absolutely_free_photos/small_photos/crowded-cars-on-street-4032x2272_48736.jpg

# create python file with folowing code and run

"""

from pydarknet import Detector, Image
import cv2
cv2.__version__

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 = cv2.imread('crowded-cars-on-street-4032x2272_48736.jpg')
img_darknet = Image(img)

results = net.detect(img_darknet)

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.imshow("output", img)
cv2.waitKey(0)


"""

INSTALLER MODELS
https://github.com/madhawav/YOLO3-4-Py/blob/master/download_models.sh