예제 #1
0
def slave_labor(frame):
    h, w, _ = frame.shape
    roi_array = []
    full_im, _ = darknet.array_to_image(frame)
    darknet.rgbgr_image(full_im)

    gpu_lock.acquire()
    if args.yolo:
        if w < h:
            spacing = int((h - w) / 2)
            roi_array = [(0, spacing, w, h - spacing)]
        else:
            spacing = int((w - h) / 2)
            roi_array = [(spacing, 0, w - spacing, h)]

    if not roi_array:
        roi_array = [(0, 0, w, h)]

    # TODO: remove frame_rois
    frame_rois = []

    nets = server_testing_internal['nets']
    plans = server_testing_internal['plans']
    groups = server_testing_internal['groups']
    metas = server_testing_internal['metas']

    results = []

    for i, _ in enumerate(nets):
        roi = roi_array[0]
        if args.yolo:
            # print(roi)
            frame_roi = frame[roi[1]: roi[3], roi[0]:roi[2]]
            frame_rois.append(frame_roi)
            if not args.socket and not args.interactive:
                cv.imshow("frame_roi", frame_roi)
        else:
            frame_roi = frame
        im, _ = darknet.array_to_image(frame_roi)
        darknet.rgbgr_image(im)
        r = lightnet.classify(nets[i], metas[i], im)

        top_k = args.top_k
        if top_k >= len(r):
            top_k = len(r)

        for rank in range(0, top_k):
            (label, score) = r[rank]
            results.append({
                'plan': plans[i], 
                'group': groups[i], 
                'predicate_sku': label,
                'score': score,
            })
    logger.info("|lightnet.classify")
    gpu_lock.release()

    return results
예제 #2
0
def slave_labor(frame):
    h, w, _ = frame.shape
    roi_array = []
    full_im, _ = darknet.array_to_image(frame)
    darknet.rgbgr_image(full_im)

    gpu_lock.acquire()
    if args.yolo:
        if w < h:
            spacing = int((h - w) / 2)
            roi_array = [(0, spacing, w, h - spacing)]
        else:
            spacing = int((w - h) / 2)
            roi_array = [(spacing, 0, w - spacing, h)]

    if not roi_array:
        roi_array = [(0, 0, w, h)]

    preds = []

    frame_rois = []

    for i, _ in enumerate(nets):
        results = [] # cross all rois
        for roi in roi_array:
            if args.yolo:
                # print(roi)
                frame_roi = frame[roi[1]: roi[3], roi[0]:roi[2]]
                frame_rois.append(frame_roi)
                if not args.socket and not args.interactive:
                    cv.imshow("frame_roi", frame_roi)
            else:
                frame_roi = frame
            im, _ = darknet.array_to_image(frame_roi)
            darknet.rgbgr_image(im)
            r = lightnet.classify(nets[i], metas[i], im) # for single roi

            results.extend(r)
        results = sorted(results, key=lambda x: -x[1])
        for rank in range(0, args.top_k):
            (label, score) = results[rank]
            preds.append({
                'plan': '100XGROUP', # TODO: remove hardcoding
                'group': args_groups[i], 
                'predicate_sku': label,
                'score': score,
            })
    logger.info("|lightnet.classify")
    gpu_lock.release()

    return preds
예제 #3
0
파일: table.py 프로젝트: mppsk0/table-ocr-1
def darknet_GPU_predict(img, prob=0.5):
    imgW, imgH = SIZE
    imgResize, fx, fy, dx, dy = letterbox_image(img, SIZE)
    im = array_to_image(imgResize)
    out = predict_image(tableNet, im)
    values = []
    for i in range(2 * imgW * imgH):
        values.append(out[i])
    out = np.array(values).reshape((2, imgH, imgW))
    #out = exp(out)
    out = out[:, dy:, dx:]
    return out, fx, fy
예제 #4
0
    def predict_image(self, image, thresh=0.25, is_show=True, save_path=''):
        '''
        :param image:    cv2.imread 图像, darknet自己会对图像进行预处理
        :param thresh:   置信度阈值, 其它阈值不变
        :param is_show:  是否将画框之后的图像返回
        :param save_path: 画框后的保存路径
        :return:         返回1个矩阵
        '''
        # bgr->rgb
        rgb_img = image[..., ::-1]
        # 获取图片大小,网络输入大小
        height, width = rgb_img.shape[:2]
        network_width = darknet.network_width(self.netMain)
        network_height = darknet.network_height(self.netMain)
        # 将图像resize到输入大小
        rsz_img = cv2.resize(rgb_img, (network_width, network_height),
                             interpolation=cv2.INTER_LINEAR)
        # 转成tensor的形式,以及[1,C,H,W]
        darknet_image, _ = darknet.array_to_image(rsz_img)
        detections = darknet.detect_image(self.netMain,
                                          self.metaMain,
                                          darknet_image,
                                          thresh=thresh)

        if is_show:
            for detection in detections:
                x, y, w, h = detection[2][0], \
                             detection[2][1], \
                             detection[2][2], \
                             detection[2][3]
                # 置信度
                conf = detection[1]
                # 预测标签
                label = detection[0].decode()
                # 获取坐标
                x *= width / network_width
                w *= width / network_width
                y *= height / network_height
                h *= height / network_height
                # 转成x1y1x2y2,左上右下坐标; x是w方向
                xyxy = np.array([x - w / 2, y - h / 2, x + w / 2, y + h / 2])

                index = self.names.index(label)
                label_conf = f'{label} {conf:.2f}'
                self._plot_one_box(xyxy, rgb_img, self.colors[index],
                                   label_conf)
            bgr_img = rgb_img[..., ::-1]
            # 保存图像
            if save_path:
                cv2.imwrite(save_path, bgr_img)

            return bgr_img  #返回画框的bgr图像
        return detections
예제 #5
0
def pipeline(img):
    # image data transform
    # img - cv image
    # im - yolo image
    im, image = dn.array_to_image(img)
    dn.rgbgr_image(im)

    tic = time.time()
    result = detect2(net, meta, im)
    toc = time.time()
    print(toc - tic, result)

    img_final = dn.draw_boxes(img, result)
    return img_final
예제 #6
0
def detect_from_file(net, meta, image_path, thresh=.5, hier_thresh=.5, nms=.45, debug=False):
    #pylint: disable= C0321
    if USING_DARKNET_IMAGE_IO:
        im = darknet.load_image(to_str(image_path, True), 0, 0)
    else:
        import cv2
        custom_image = cv2.imread(to_str(image_path))
        im, arr = darknet.array_to_image(custom_image)
    if debug:
        print("Loaded image")
    det = detect_from_memory(net, meta, im, thresh, hier_thresh, nms, debug)
    if USING_DARKNET_IMAGE_IO:
        darknet.free_image(im)
        if debug:
            print("freed image")

    return det
예제 #7
0
    def __init__(self, **properties):
        super(SensorFactory, self).__init__(**properties)
        global frame, buffer_frames, buffer_index
        ret, frame = cap.read()
        im = dn.array_to_image(frame)
        dn.rgbgr_image(im)
        buffer_frames = [im] * 3
        buffer_index = 0

        self.number_frames = 0
        self.fps = 60
        self.duration = (
            1.0 / self.fps) * Gst.SECOND  # duration of a frame in nanoseconds
        self.launch_string = ('appsrc name=source is-live=true block=true format=GST_FORMAT_TIME ' \
                             'caps=video/x-raw,format=BGR,width=320,height=240,framerate={}/1 ' \
                             '! videoconvert ! video/x-raw,format=I420 ' \
                             '! x264enc speed-preset=ultrafast tune=zerolatency ' \
                             '! rtph264pay config-interval=1 name=pay0 pt=96').format(self.fps)
예제 #8
0
    def convert_image(self, image):
        """
        Convert numpy array to specific format which can be used by darknet library.

        Parameters
        ----------
        image: numpy array
            a three-dimensional array with uint type

        Returns
        -------
        im: custom object
            an object which is defined by darknet library
        """
        resized_image = cv2.resize(image, (self.width, self.height),
                                   interpolation=cv2.INTER_LINEAR)
        im, _ = dn.array_to_image(resized_image)
        return im
예제 #9
0
def detect(img):
    bgr_img = img[:, :, ::-1]
    height, width = bgr_img.shape[:2]
    rsz_img = cv.resize(bgr_img, (darknet.network_width(netMain), darknet.network_height(netMain)),
                     interpolation=cv.INTER_LINEAR)
    darknet_image, _ = darknet.array_to_image(rsz_img)
    detections = darknet.detect_image(netMain, metaMain, darknet_image, thresh=0.25)
    
    index = -1
    maxconf = -1.0
    # 取置信度最大的那个值
    for i, item in enumerate(detections):
        if item[1] > maxconf:
            index = i
            maxconf = item[1]
    if index != -1:     #有检测结果
        detection = detections[index]
        label = detection[0].decode()
        conf = detection[1]
        label = f'{label} {conf:.2f}'
        return label
    
    return None
    '''
예제 #10
0
def detect(im):
    """im: opencv format image data"""
    im = darknet.array_to_image(im)
    darknet.rgbgr_image(im)
    return darknet.detect(net, meta, im)
예제 #11
0
파일: obj_detect.py 프로젝트: yaolu/darknet
import darknet as dn
from tqdm import tqdm
from matplotlib.image import imread

if __name__ == "__main__":
    #    dn.set_gpu(0)
    net = dn.load_net(str.encode("data/yolov3-tiny.cfg"),
                      str.encode("data/yolov3-tiny.weights"), 0)
    meta = dn.load_meta(str.encode("data/coco.data"))
    r = []
    # for speed benchmark
    img, _ = dn.array_to_image(imread("data/dog.jpg"))
    r = dn.detect(net, meta, img, thresh=.2)
예제 #12
0
def YOLO():

    global metaMain, netMain, altNames
    configPath = "./cfg/yolov4-tiny.cfg"
    weightPath = "./yolov4-tiny.weights"
    metaPath = "./cfg/coco.data"
    if not os.path.exists(configPath):
        raise ValueError("Invalid config path `" +
                         os.path.abspath(configPath)+"`")
    if not os.path.exists(weightPath):
        raise ValueError("Invalid weight path `" +
                         os.path.abspath(weightPath)+"`")
    if not os.path.exists(metaPath):
        raise ValueError("Invalid data file path `" +
                         os.path.abspath(metaPath)+"`")
    if netMain is None:
        netMain = darknet.load_net_custom(configPath.encode(
            "ascii"), weightPath.encode("ascii"), 0, 1)  # batch size = 1
    if metaMain is None:
        metaMain = darknet.load_meta(metaPath.encode("ascii"))
    if altNames is None:
        try:
            with open(metaPath) as metaFH:
                metaContents = metaFH.read()
                import re
                match = re.search("names *= *(.*)$", metaContents,
                                  re.IGNORECASE | re.MULTILINE)
                if match:
                    result = match.group(1)
                else:
                    result = None
                try:
                    if os.path.exists(result):
                        with open(result) as namesFH:
                            namesList = namesFH.read().strip().split("\n")
                            altNames = [x.strip() for x in namesList]
                except TypeError:
                    pass
        except Exception:
            pass
    #cap = cv2.VideoCapture(0)
    darknet_image = darknet.make_image(darknet.network_width(netMain),
                                       darknet.network_height(netMain),3)
    # 连接Basler相机列表的第一个相机
    camera = pylon.InstantCamera(pylon.TlFactory.GetInstance().CreateFirstDevice())

    # 开始读取图像
    camera.StartGrabbing(pylon.GrabStrategy_LatestImageOnly)
    converter = pylon.ImageFormatConverter()
    #输出源视频
    out = cv2.VideoWriter(
             "/home/nvidia/Object_Detect/original_video/%s_origital.avi"%(time.strftime('%Y-%m-%d_%H_%M_%S')), cv2.VideoWriter_fourcc(*"MP42"), 20.0,
             (darknet.network_width(netMain), darknet.network_height(netMain)))
    out_detect = cv2.VideoWriter(
        "/home/nvidia/Object_Detect/detect_video/%s_detect.avi"%(time.strftime('%Y-%m-%d_%H_%M_%S')), cv2.VideoWriter_fourcc(*"MP42"), 20.0,
        (darknet.network_width(netMain), darknet.network_height(netMain)))
    # 转换为OpenCV的BGR彩色格式
    converter.OutputPixelFormat = pylon.PixelType_BGR8packed
    converter.OutputBitAlignment = pylon.OutputBitAlignment_MsbAligned


    while camera.IsGrabbing():
        prev_time = time.time()
        grabResult = camera.RetrieveResult(5000, pylon.TimeoutHandling_ThrowException)

        if grabResult.GrabSucceeded():
            # 转换为OpenCV图像格式
            image = converter.Convert(grabResult)
            img = image.GetArray()
            # darknet 处理部分

            frame_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
            frame_resized = cv2.resize(frame_rgb,
                                       (darknet.network_width(netMain),
                                        darknet.network_height(netMain)),
                                       interpolation=cv2.INTER_LINEAR)

            darknet.copy_image_from_bytes(darknet_image, frame_resized.tobytes())

            detections = darknet.detect_image(netMain, metaMain, darknet_image, thresh=0.25)
            frame_resized_color = cv2.cvtColor(frame_resized, cv2.COLOR_BGR2RGB)
            out.write(frame_resized_color)
            image,box_num = cvDrawBoxes(detections, frame_resized)
            cv2.imwrite('/home/nvidia/Object_Detect/temp.jpg', image)
            image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
            # 缓存

            #img=darknet.change_cvmat("/home/nvidia/Object_Detect/image/2020-07-06_17_29_46.jpg",1)
            darknet.send_mjpeg('/home/nvidia/Object_Detect/temp.jpg'.encode("utf8"), 8090, 400000, 40)
            print("转换成功")
            im, arr = darknet.array_to_image(image)
            #print(box_num)



            save_detections(box_num,image)
            out_detect.write(image)
            #输出fps
            #print(1 / (time.time() - prev_time))

            cv2.imshow('Demo', image)
            k = cv2.waitKey(1)
            if k == 27:
                break
        grabResult.Release()


    # 关闭相机
    camera.StopGrabbing()
    # 关闭窗口
    cv2.destroyAllWindows()
    out.release()
    out_detect.release()
예제 #13
0
                    with open(result) as namesFH:
                        namesList = namesFH.read().strip().split("\n")
                        altNames = [x.strip() for x in namesList]
            except TypeError:
                pass
    except Exception:
        pass

image_name = '/home/weizy/Programs/YOLOv4/darknet/data/dog.jpg'
src_img = cv2.imread(image_name)
bgr_img = src_img[:, :, ::-1]
height, width = bgr_img.shape[:2]
rsz_img = cv2.resize(
    bgr_img, (darknet.network_width(netMain), darknet.network_height(netMain)),
    interpolation=cv2.INTER_LINEAR)
darknet_image, _ = darknet.array_to_image(rsz_img)
detections = darknet.detect_image(netMain,
                                  metaMain,
                                  darknet_image,
                                  thresh=0.25)


# convert xywh to xyxy
def convert_back(x, y, w, h):
    xmin = int(round(x - (w / 2)))
    xmax = int(round(x + (w / 2)))
    ymin = int(round(y - (h / 2)))
    ymax = int(round(y + (h / 2)))
    return xmin, ymin, xmax, ymax

예제 #14
0
def slave_labor(frame):
    h, w, _ = frame.shape
    roi_array = []

    gpu_lock.acquire()
    if args.yolo:
        if w < h:
            spacing = int((h - w) / 2)
            roi_array = [(0, spacing, w, h - spacing)]
        else:
            spacing = int((w - h) / 2)
            roi_array = [(spacing, 0, w - spacing, h)]

    if not roi_array:
        roi_array = [(0, 0, w, h)]

    results_hier = []
    results_flat = []

    frame_rois = []

    for i, _ in enumerate(nets):
        results = []
        for roi in roi_array:
            if args.yolo:
                # print(roi)
                frame_roi = frame[roi[1]:roi[3], roi[0]:roi[2]]
                frame_rois.append(frame_roi)
                if not args.socket and not args.interactive:
                    cv.imshow("frame_roi", frame_roi)
            else:
                frame_roi = frame
            im, _ = darknet.array_to_image(frame_roi)
            darknet.rgbgr_image(im)
            r = lightnet.classify(nets[i], metas[i], im)

            results.extend(r)
            results_flat.extend(r)
            # results = sorted(results, key=lambda x: -x[1])
        results_hier.append(results)
    logger.info("|lightnet.classify")
    gpu_lock.release()

    results_flat = sorted(results_flat, key=lambda x: -x[1])
    top_k = args.top_k
    if top_k >= len(results_flat):
        top_k = len(results_flat)

    preds = []
    for rank in range(0, top_k):
        left = 10
        top = 20 + rank * 20
        (label, score) = results_flat[rank]
        if score >= args.threshold:
            preds.append((label, score))

        text = '%s %.2f%%' % (label, score * 100)
        labelSize, baseLine = cv.getTextSize(text, cv.FONT_HERSHEY_SIMPLEX,
                                             0.5, 1)
        back_clr = (222, 222, 222)
        if score > args.gold_confidence:
            back_clr = (122, 122, 255)
        cv.rectangle(frame, (left, top - labelSize[1]),
                     (left + labelSize[0], top + baseLine), back_clr,
                     cv.FILLED)

        cv.putText(frame, text, (left, top), cv.FONT_HERSHEY_SIMPLEX, 0.5,
                   (0, 0, 0))

    if args.socket:
        if args.debug:
            now = datetime.datetime.now()
            now_string = now.strftime("%Y-%h-%d-%H-%M-%S-%f")
            image_name = 'socket_debug' + '/' + now_string + '.jpg'
            cv.imwrite(image_name, frame)
            csv_file.write(image_name)
            for results in results_hier:
                top_k = 3
                for rank in range(0, top_k):
                    (label, score) = results[rank]
                    csv_file.write(',%s,%.3f' % (label[4:], score))
            csv_file.write('\n')
            csv_file.flush()

            logger.info("|csv_file.write")

    elif args.interactive:
        pass
    else:
        cv.imshow("output", frame)

    return preds
예제 #15
0
path_v = '/home/julien/Videos'

files = os.listdir(path_v)
i = 1
for f in files:
    vcap = cv2.VideoCapture(path_v + "/" + f)

    while vcap.isOpened():
        ret, frame = vcap.read()
        if not ret:
            print('end')
            break
        custom_image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        #custom_image = cv2.resize(custom_image,(dn.lib.network_width(net), dn.lib.network_height(net)), interpolation = cv2.INTER_LINEAR)
        image, arr = dn.array_to_image(custom_image)
        result = dn.detect(net, meta, image, thresh=0.2)
        if len(result) != 0:
            print("Recognize !!!!!!!!!!!!!!!!!!")
            for p in result:
                x1 = int(p[2][0] - p[2][2] / 2)
                y1 = int(p[2][1] - p[2][3] / 2)
                x2 = int(p[2][0] + p[2][2] / 2)
                y2 = int(p[2][1] + p[2][3] / 2)
                #cv2.rectangle(frame,(x1,y1),(x2,y2),(0,255,0),3)
                #cv2.putText(frame, p["plate"], (x1,y1-5), cv2.FONT_HERSHEY_SIMPLEX, 1,(0,255,0),2)
                h, w = frame.shape[:2]
                center = (p[2][0], p[2][1])
                hb, wb = (p[2][3], p[2][2])
                #cv2.circle(frame, center, 10, (255,0,0),2)
                #cv2.line(frame, (center[0]-wb/2,center[1]), (center[0]+wb/2,center[1]), (255,0,0),3)
def process_frame():
    process_this_frame = True
    while True:
        ret, frame = video_capture.read()
        if ret == False:
            continue
        img_ori = Image.fromarray(cv2.cvtColor(frame,
                                               cv2.COLOR_BGR2RGB))  # Image
        height, width, _ = frame.shape
        scale_width = round(int(width) / 4096, 2)
        scale_height = round(int(height) / 2160, 2)
        img_IM, _ = dn.array_to_image(frame)  # im:IMAGE
        start1 = perf_counter()
        res_img = dn.detect(net2,
                            meta,
                            img_IM,
                            thresh=.5,
                            hier_thresh=.5,
                            nms=.45,
                            debug=False)
        end1 = perf_counter()
        print("yolo检测耗时:" + str(end1 - start1))
        print(res_img)

        # img_ori = np.array(Image.open('./images/kuaile.jpg'))    # image
        # height, width, _ = img_ori.shape
        # img_IM, _ = dn.array_to_image(img_ori)  # IMAGE
        # res_img = dn.detect(net2, meta, img_IM, thresh=.5, hier_thresh=.5, nms=.45, debug=False)
        # print(res_img)

        if len(res_img) != 0:
            for i in range(len(res_img)):
                if res_img[i][0] == b'person':
                    pos = res_img[i][2]
                    x = pos[0]
                    y = pos[1]
                    w = pos[2]
                    h = pos[3]
                    left = x - w / 2  # x0
                    right = x + w / 2  # x1
                    top = y - h / 2  # y0
                    bottom = y + h / 2  # y1
                    img_ori = np.array(img_ori)  # 切割前转换成array

                    cropped = img_ori[int(top):int(bottom),
                                      int(left):int(right)]

                    gray = rgb2gray(cropped)
                    gray = np.asarray(gray)
                    gray = cv2.resize(gray, (48, 48))
                    img_cropped = gray[:, :, np.newaxis]
                    img_cropped = np.concatenate(
                        (img_cropped, img_cropped, img_cropped), axis=2)
                    img_cropped = Image.fromarray(np.uint8(img_cropped))

                    inputs = transform_test(img_cropped)
                    class_names = [
                        'Angry', 'Disgust', 'Fear', 'Happy', 'Sad', 'Surprise',
                        'Neutral'
                    ]
                    ncrops, c, h, w = np.shape(inputs)

                    inputs = inputs.view(-1, c, h, w)
                    inputs = inputs.cuda()
                    with torch.no_grad():
                        inputs = Variable(inputs)
                    outputs = net(inputs).to(device)

                    outputs_avg = outputs.view(ncrops,
                                               -1).mean(0)  # avg over crops
                    print("输出概率:" + str(outputs_avg))

                    _, predicted = torch.max(outputs_avg.data, 0)
                    print("输出预测结果:" + str(predicted))
                    emojis_img = io.imread('images/emojis/%s.png' % str(
                        class_names[int(predicted.to(device).cpu().numpy())]))

                    cv2.rectangle(img_ori, (int(left), int(top)),
                                  (int(right), int(bottom)), (135, 120, 28), 2)
                    cv2.rectangle(img_ori,
                                  (int(left), int(top - 100 * scale_height)),
                                  (int(right), int(top)), (35, 235, 185), -1)
                    font = cv2.FONT_HERSHEY_DUPLEX
                    cv2.putText(
                        img_ori,
                        class_names[int(predicted.to(device).cpu().numpy())],
                        (int(x - 60 * scale_width),
                         int(top - 10 * scale_height)), font,
                        3.0 * scale_height, (30, 41, 61), 1)
                    cv2.imshow('emoji', emojis_img)
            img_ori = cv2.cvtColor(np.array(img_ori),
                                   cv2.COLOR_BGR2RGB)  # 从BGR转回RGB
            cv2.imshow('video', img_ori)
            if cv2.waitKey(1) & 0xFF == ord('q'):
                break
        else:
            img_ori = cv2.cvtColor(np.array(img_ori), cv2.COLOR_BGR2RGB)
            cv2.imshow('video', img_ori)
            if cv2.waitKey(1) & 0xFF == ord('q'):
                break
    #     out.write(frame)
    # cap.release()
    # out.release()
    video_capture.release()
    cv2.destroyAllWindows()
예제 #17
0
def process(category):
    image_filenames = glob.glob('%s/*.png' % (category), recursive=True)
    image_filenames.extend(glob.glob('%s/*.jpg' % (category), recursive=True))
    # print("Start category: %s" % (category))
    for filename in image_filenames:
        # load the input image
        filename = pathlib.Path(filename).as_posix()
        image = cv.imread(filename)
        h, w, c = image.shape

        # roi
        if False:
            x_spacing = 20
            image = image[0:h, x_spacing:w - x_spacing]
        h, w, c = image.shape

        if args.debug:
            print(w, h)

        x1, y1, x2, y2 = (0, 0, w, h)
        if args.yolo_cfg:
            full_im, _ = darknet.array_to_image(image)
            with inference_lock:
                results = lightnet.detect_from_memory(
                    yolo_net, yolo_meta, full_im, thresh=0.75, debug=False)
            if results:
                detection = results[0]
                x, y, w, h = detection[2][0],\
                    detection[2][1],\
                    detection[2][2],\
                    detection[2][3]
                if w > W and h > H:
                    x1, y1, x2, y2 = lightnet.convertBack(
                        float(x), float(y), float(w), float(h))
        else:
            (success, saliencyMap) = saliency.computeSaliency(image)
            M = cv.moments(saliencyMap.astype("uint8"))

            # calculate x,y coordinate of center
            cX = int(M["m10"] / M["m00"])
            cY = int(M["m01"] / M["m00"])
            cv.circle(saliencyMap, (cX, cY), 5, (255, 255, 255), -1)

            cY = h / 2
            if cX < W / 2:
                cX = W / 2
            if cX > w - W / 2:
                cX = w - W / 2
            x1 = int(cX - W / 2)
            y1 = int(cY - H / 2)
            x2 = int(cX + W / 2)
            y2 = int(cY + H / 2)

        filename = filename.replace(
            args.images, 'img_roi').replace('.png', '.jpg')
        # print(filename)
        os.makedirs(os.path.dirname(filename), exist_ok=True)
        cv.imwrite(filename, image[y1:y2, x1:x2])

        if args.debug:
            # show the images
            cv.rectangle(saliencyMap, (x1, y1), (x2, y2), (255, 255, 255), 1)
            cv.imshow("Image", image)
            cv.imshow("Output", saliencyMap)

            key = cv.waitKey(0)
            if key == 27:
                break

    print("Finish category: %s" % (category))
예제 #18
0
def read_frame():
    global frame
    ret, frame = cap.read()
    im = dn.array_to_image(frame)
    dn.rgbgr_image(im)
    buffer_frames[(buffer_index + 1) % 3] = im
예제 #19
0
import darknet

IMAGE_MODE = False

if __name__ == "__main__":
    lightnet.set_cwd(dir)

    net, meta = lightnet.load_network_meta("obj.cfg",
                                           "weights/obj_200.weights",
                                           "obj.data")
    # "../../bin/cfg/darknet19_448.cfg", "../../bin/darknet19_448.weights", "../../bin/cfg/imagenet1k.data")

    if IMAGE_MODE:
        if True:
            frame = cv.imread(lightnet.to_str('test.jpg'))
            im, arr = darknet.array_to_image(frame)
            darknet.rgbgr_image(im)
        else:
            im = darknet.load_image(
                lightnet.to_str('test.jpg').encode("ascii"), 0, 0)

        r = darknet.classify(net, meta, im)
        print(r)
    else:
        cap = cv.VideoCapture(0)
        if not cap.isOpened():
            raise Exception('Fail to open %s' % (0))
        while True:
            hasFrame, frame = cap.read()
            if not hasFrame:
                cv.waitKey()