Exemplo n.º 1
0
def find_name(crop_gray, crop_org):
    template = cv2.UMat(
        cv2.imread('%s/studentcard/mask/name_mask.jpg' % (currentPath), 0))
    # showimg(crop_org)
    w, h = cv2.UMat.get(template).shape[::-1]
    res = cv2.matchTemplate(crop_gray, template, cv2.TM_CCOEFF_NORMED)
    min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
    print max_loc
    top_left = (max_loc[0] + w, max_loc[1] - int(20 * x))
    bottom_right = (top_left[0] + int(1000 * x), top_left[1] + int(400 * x))
    result = cv2.UMat.get(crop_org)[top_left[1]:bottom_right[1],
                                    top_left[0]:bottom_right[0]]
    cv2.rectangle(crop_gray, top_left, bottom_right, 255, 2)
    # showimg(result)
    return cv2.UMat(result)
Exemplo n.º 2
0
 def calculate_masked_circle(self, img: cv2.UMat, coordinates: tuple,
                             radius: int) -> cv2.UMat:
     """
     Calculate the mask for the foveated area and the peripheral area
     :param img: original image
     :param coordinates: coords of the gaze where the foveated area shall be cropped out
     :param radius: radius of the foveated area
     :return: masked foveated area, masked peripheral area
     """
     mask_foveated = cv2.UMat(np.zeros(self.size[::-1], dtype=np.uint8))
     cv2.circle(mask_foveated, coordinates, radius, (255, 255, 255), -1, 0,
                0)
     masked_image = cv2.bitwise_or(img, img, mask=mask_foveated)
     mask_peripheral = cv2.UMat(np.zeros(self.size[::-1], dtype=np.uint8))
     return masked_image, mask_peripheral
Exemplo n.º 3
0
def plot_non_max_suppression(numpy_img, preds, label, color):
    """ non max suppression + duplicates suppression"""
    def suppress(box, boxes, indexes):
        ious_50 = [
            i for i in indexes
            if intersection_over_union(box, boxes[i]) >= 0.50
        ]
        duplicates = [
            i for i in indexes
            if intersection_over_b_area(box, boxes[i]) >= 0.91
        ]
        for i in set(duplicates).union(set(ious_50)):
            indexes.remove(i)

    labels = preds["labels"].detach().numpy()
    scores = preds["scores"].detach().numpy()
    boxes = preds["boxes"].detach().numpy()[(labels == label)
                                            & (scores >= 0.55)]
    indexes = list(np.arange(len(boxes)))
    result_boxes = []
    # suppression algorithm
    while len(indexes) > 0:
        box = boxes[indexes[0]]
        indexes.remove(indexes[0])
        suppress(box, boxes, indexes)
        result_boxes.append(box)
    # adding rectangles
    nimg = cv2.UMat(numpy_img)
    for box in result_boxes:
        nimg = cv2.rectangle(nimg, (box[0], box[1]), (box[2], box[3]),
                             color=color,
                             thickness=2)
    return (result_boxes, nimg.get())
    def annotated_frame(self,
                        annotate_temperature=True,
                        annotate_breath_rate=True):
        """ Returns a grey frame with annotation, used for visualization.

        Args:
            annotate_temperature: Whether the body temperature should be annotated 
                on the returned frame.
            annotate_breath_rate: Whether the breath rate should be annotated on 
                the returned frame.
        """
        annotated_frame = cv2.UMat(self.grey_frame)
        for face in self.thermal_faces:
            cv2.rectangle(annotated_frame, tuple(face.bounding_box[:2]),
                          tuple(face.bounding_box[2:]), (255, 0, 0), 1)
            if annotate_temperature:
                temperature = face.temperature
                if temperature is not None:
                    cv2.putText(annotated_frame,
                                str(temperature)[:5] + ' C',
                                tuple(face.bounding_box[:2]),
                                cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 1)
            if annotate_breath_rate:
                breath_rate = face.breath_rate
                if breath_rate is not None:
                    cv2.putText(annotated_frame,
                                str(breath_rate)[:5] + 'Hz',
                                (face.bounding_box[0], face.bounding_box[3]),
                                cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 1)
        return annotated_frame
Exemplo n.º 5
0
 def run(self,
         feed,
         visualize_temperature=True,
         visualize_breath_rate=True,
         visualize_breath_curve=True):
     for raw_frame, timestamp in feed:
         thermal_frame = ThermalFrame(raw_frame, timestamp)
         if len(self.thermal_frame_queue) > 0:
             thermal_frame.link(self.thermal_frame_queue[-1])
         if len(self.thermal_frame_queue) >= config.MAX_CACHED_FRAMES:
             self.thermal_frame_queue.pop(0)
             self.thermal_frame_queue[0].detach()
         self.thermal_frame_queue.append(thermal_frame)
         annotation_frame = cv2.UMat(
             np.stack([thermal_frame.grey_frame] * 3, 2))
         self._visualize_bounding_boxes(annotation_frame,
                                        thermal_frame.thermal_faces)
         if visualize_temperature:
             self._visualize_temperatures(annotation_frame,
                                          thermal_frame.thermal_faces)
         if visualize_breath_rate:
             self._visualize_breath_rates(annotation_frame,
                                          thermal_frame.thermal_faces)
         if visualize_breath_curve:
             self._visualize_breath_curves(thermal_frame.thermal_faces)
         cv2.imshow(
             'thermal monitoring',
             cv2.resize(annotation_frame, config.VISUALIZATION_RESOLUTION))
         if cv2.waitKey(1) & 0xFF == ord('q'):
             break
     cv2.destroyAllWindows()
Exemplo n.º 6
0
def get_domains(img, contour_level=0.5, min_area=1, max_area=100000):
    """Get crystallite domain information extracted from an image.
    Use a highly contrasted image, such as one output from the
    'get_kmeans_map' function. areas are calculated. Returns a dictionary of countour lines which
    define each cluster domain and area of each domain."""
    # extract contour line ordered pairs from the map
    contours = skimage.measure.find_contours(img, contour_level)
    # calculate area of inside each contour line
    areas = [
        cv2.contourArea(cv2.UMat(np.expand_dims(c.astype(np.float32), 1)))
        for c in contours
    ]
    # create zipped list of areas and contour lines below area threshold
    zipped = [i for i in zip(areas, contours) if min_area <= i[0] <= max_area]
    # sort from large area to small area domains
    zipped.sort(key=lambda x: x[0], reverse=True)
    # get domain statistics
    tot_area = img.shape[0] * img.shape[1]
    tot_domain_area = np.sum([z[0] for z in zipped])
    domain_percent = 100 * tot_domain_area / tot_area
    # create dictionary to hold results
    domains = {
        'tot_area': int(tot_area),
        'tot_domain_area': int(tot_domain_area),
        'domain_percent': domain_percent,
        'areas': [z[0] for z in zipped],
        'contours': [np.flip(z[1]) for z in zipped],
        'mean_x': [np.mean(z[1][:, 1]) for z in zipped],
        'mean_y': [np.mean(z[1][:, 0]) for z in zipped]
    }
    return domains
Exemplo n.º 7
0
    def draw_boxes(self, images, bboxes, labels):
        label_dict = {0: "Cat", 1: "Dog"}

        for batch in zip(images, bboxes, labels):
            cv2.destroyAllWindows()
            image, bbox, label = batch[0].cpu().numpy(), batch[1].cpu().numpy(), torch.argmax(batch[2]).cpu().item()
            image = np.rollaxis(image, 0, 3)
            image = ((image - image.min()) * (1 / (image.max() - image.min()) * 255)).astype("uint8")
            image = cv2.UMat(image)

            cv2.rectangle(image, (bbox[0], bbox[1]), (bbox[2], bbox[3]), (0, 255, 0), thickness=2)

            cv2.putText(
                image,
                f"{label_dict[label]}",
                (bbox[1], bbox[3]),
                cv2.FONT_HERSHEY_SIMPLEX,
                0.5,
                (0, 255, 0),
                1,
                cv2.LINE_AA,
            )
            cv2.imshow("test", image)
            cv2.waitKey(1)
            sleep(1)
            cv2.destroyAllWindows()
Exemplo n.º 8
0
def find_idnum(crop_gray, crop_org):
    template = cv2.UMat(
        cv2.imread('%s/idcard/mask/idnum_mask_%s.jpg' % (currentPath, pixel_x),
                   0))
    # showimg(template)
    #showimg(crop_gray)
    w, h = cv2.UMat.get(template).shape[::-1]
    res = cv2.matchTemplate(crop_gray, template, cv2.TM_CCOEFF_NORMED)
    min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
    top_left = (max_loc[0] + w, max_loc[1] - int(20 * x))
    bottom_right = (top_left[0] + int(2300 * x), top_left[1] + int(300 * x))
    result = cv2.UMat.get(crop_org)[top_left[1]:bottom_right[1],
                                    top_left[0]:bottom_right[0]]
    cv2.rectangle(crop_gray, top_left, bottom_right, 255, 2)
    #showimg(crop_gray)
    return cv2.UMat(result)
def findCollision(folder, is_debug):
    # folder = './dataset/train/toothpaste_box/21/'
    before = 0
    after = 1
    row = 0
    col = 1
    mask_img_files = sorted(glob.glob(f'{folder}mask/*.png'))
    mask_img = np.array([plt.imread(mask_img_files[0]),
                         plt.imread(mask_img_files[-1])])

    center_before, cnt = findContourCenter(mask_img[before])
    center_after, cnt = findContourCenter(mask_img[after])

    distance = math.sqrt((center_after[row] - center_before[row])
                         ** 2 + (center_after[col] - center_before[col])**2)
    angle = math.atan2((center_after[row] - 220),
                       (center_after[col] - 220))
    if is_debug:
        print(folder)
        print(f'angle = {angle}', f'distance = {distance}')
        inter_img = np.array([mask_img[before], mask_img[after],
                              np.zeros(mask_img[after].shape)])
        inter_img = np.moveaxis(inter_img, 0, -1)
        inter_img = cv2.UMat(inter_img)
        inter_img = cv2.UMat.get(inter_img)
        cv2.drawContours(inter_img, [cnt], -1, (0, 0, 255), 2)
        plt.imshow(inter_img)
        plt.plot([center_before[1], center_after[1]],
                 [center_before[0], center_after[0]])
        plt.plot(center_after[1],
                 center_after[0], marker='o')
        plt.show()

    return angle, distance
Exemplo n.º 10
0
def get_address(img,handle):
        print('address')
        _, _, red = cv2.split(img)
        red = cv2.UMat(red)
        red = hist_equal(red)
        #showing(red,'red0')
        #red=cv2.bilateralFilter(red,0,s,d)
        #red=cv2.ximgproc.guidedFilter(red,red,50,20,-5)
        #showing(red,'red1')
        red = cv2.adaptiveThreshold(red, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,135,40)
       
        #print(pytesseract.image_to_string(red, lang='chi_sim', config='-psm 6'))
        #red=cv2.dilate(red,np.ones((3,3),np.uint8),1)
        #red=cv2.erode(red,np.ones((5,5),np.uint8),3)
        #red = cv2.medianBlur(red,3)
        #red=cv2.GaussianBlur(red, (3, 3),200)#高斯滤波
        #red=cv2.boxFilter(red, -1, (3, 3), normalize =1)#方框滤波,效果差
        #red=cv2.blur(red, (3, 3))#均值滤波
        
        #showing(red,'red2')
        red = img_resize(red, 300)
        img = img_resize(img, 300)
        address= punc_filter(get_result_vary_length(red,handle, 'chi_sim', img, '--psm 6'))
        print(address)
        return address
Exemplo n.º 11
0
def manually_get_centroid(img, preprocessed=False):
    global mask_watershed
    if isinstance(img, cv2.UMat):
        img = img.get().astype(int)
    else:
        img = img.astype(int)
    if not preprocessed:
        img = preprocess(img)
    mask_watershed = np.zeros(img.shape[0:2])
    print("CLic on ROI, then press y if the centroid is correct, n to reset")
    while (True):
        cv2.namedWindow("image")
        cv2.setMouseCallback("image", watershed, img)
        img_disp = cv2.cvtColor(img.astype("uint8"), cv2.COLOR_HSV2BGR)
        cv2.imshow("image", img_disp)
        cv2.imshow(
            "masked",
            cv2.bitwise_and(img_disp,
                            img_disp,
                            mask=mask_watershed.astype("uint8")))
        key = cv2.waitKey(1)
        if key & 0XFF == ord('y'):
            centroid, ret = getCentroid(cv2.UMat(mask_watershed))
            break
        if key & 0XFF == ord('n'):
            mask_watershed = np.zeros(img.shape[0:2])
    cv2.destroyWindow("image")
    cv2.destroyWindow("masked")
    return centroid
Exemplo n.º 12
0
def generate_preview(dataset_filepath, output_dir):
    h5file = h5py.File(dataset_filepath, "r")
    images = h5file["imgs"]
    num_samples = len(images)

    idx = np.random.randint(num_samples)
    images = images[idx]
    actions = h5file["actions"][idx]

    text_x = 6
    text_y = 10
    text_images = []
    color = 0
    for action, image in zip(actions, images):
        if image.shape[-1] > 4:
            image = image.transpose(1, 2, 0)
        image = cv2.UMat(image)

        action_text = decode_action(h5file.attrs["action_keys"], action)
        text_images += [
            cv2.putText(img=image,
                        text=action_text,
                        org=(text_x, text_y),
                        fontFace=1,
                        fontScale=1,
                        color=color,
                        thickness=1).get()
        ]

    output_filepath = os.path.join(output_dir, "{}.gif".format(idx))
    imageio.mimsave(output_filepath, text_images)
Exemplo n.º 13
0
 def __process_frame(self, frame):
     frame = cv2.UMat(frame)
     frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
     with torch.no_grad():
         frame = cv2.UMat.get(frame)
         frame = torch.tensor(frame, dtype=torch.float32)
         if self.device:
             frame = frame.to(self.device)
         frame = frame.unsqueeze(0)
         if (frame.size(1) == 1920 and frame.size(2) == 1080):
             frame = frame.permute(0, 3, 1, 2)
         elif (frame.size(2) == 1920 and frame.size(1) == 1080):
             frame = frame.permute(0, 3, 2, 1)
         elif (frame.size(1) == 1280 and frame.size(2) == 720):
             frame = frame.permute(0, 3, 1, 2)
             frame = functional.interpolate(frame, size=(1920, 1080))
         elif (frame.size(2) == 1280 and frame.size(1) == 720):
             frame = frame.permute(0, 3, 2, 1)
             frame = functional.interpolate(frame, size=(1920, 1080))
         else:  # if some other size, will be some stretching etc but w/e
             frame = frame.permute(0, 3, 2, 1)
             frame = functional.interpolate(frame, size=(1920, 1080))
         frame = frame.squeeze(0)
         frame = frame / 255.
         if (self.encoder):
             encoded = self.encoder(frame)[0]
             encoded = encoded.view(-1)
             return encoded
         else:
             return frame
Exemplo n.º 14
0
def frame(name):
    if request.method == 'POST':
        content = request.json
        hashtable = json.loads(content)
        time_stamp = hashtable["time_stamp"]
        frame_type = hashtable["frame_type"]
        number = hashtable["number"]
        frame = hashtable["frame"]
        frame = cv2.UMat(np.array(frame, dtype=np.uint8))
        if not os.path.exists('dataLake/%s_%s'%(name, frame_type)):
            os.makedirs('dataLake/%s_%s'%(name, frame_type))
        if not os.path.exists('dataLake/%s_%s/%s'%(name, frame_type, time_stamp)):
            os.makedirs('dataLake/%s_%s/%s'%(name, frame_type, time_stamp))
        cv2.imwrite('dataLake/%s_%s/%s/%s.jpg'%(name, frame_type, time_stamp, number), frame)
        return ('', 204)
    if request.method == 'GET':
        content = request.json
        hashtable = json.loads(content)
        time_stamp = hashtable["time_stamp"]
        frame_type = hashtable["frame_type"]
        number = hashtable["number"]
        frame = cv2.imread('dataLake/%s_%s/%s/%s.jpg'%(name, frame_type, time_stamp, number))
        print('dataLake/%s_%s/%s/%s.jpg'%(name, frame_type, time_stamp, number))
        hashtable = {"frame": frame.tolist()}
        content = json.dumps(hashtable)
        return content
Exemplo n.º 15
0
    def run(self):
        start = timeit.default_timer()

        for region in self.region_to_cut:
            for rotate in self.rotate:
                for side in self.sides:
                    self.reader = None
                    try:
                        self.reader = PdfFileReader(self.pdf_path)
                    except PyPdfError:
                        raise PyPdfError
                    except FileNotFoundError:
                        raise FileNotFoundError
                    except PermissionError:
                        raise PermissionError

                    self.cut_region(side=side, rotate=rotate, region=region)
                    self.save_cropped_bytes_pdf()
                    self.pdf_to_image()

                    for degree in self.degrees:
                        self.threshold(degree=degree)
                        self.find_qrcode()
                        self.image = cv2.UMat(self.image)
                        if self.decoded_text is not None:
                            self.total_time = timeit.default_timer() - start
                            return

        self.total_time = timeit.default_timer() - start
Exemplo n.º 16
0
def make_video_ci(src,traj,pred,tgt,n_logged,logwandb=True, display_frame_nr=True):
    seq_len = tgt.shape[1]

    srcs = np.concatenate([s for s in ((src.permute(0, 2, 3, 1).cpu().numpy()) * 255.).astype(np.uint8)[:n_logged]],axis=1)

    traj_vis = []
    for t in range(traj.shape[1]):

        arrows = draw_arrow(traj[:n_logged,t].cpu().numpy())
        traj_vis.append(arrows)

    traj_vis = np.stack(traj_vis,axis=0)
    traj_vis = put_text_to_video_row(traj_vis, "Flow Vectors", display_frame_nr=display_frame_nr)

    srcs = cv2.UMat.get(cv2.putText(cv2.UMat(srcs), f"Sequence length {seq_len}", (int(srcs.shape[1] // 3), int(srcs.shape[0] / 6)), cv2.FONT_HERSHEY_SIMPLEX,
                                    float(srcs.shape[0] / 256), (255, 0, 0), int(srcs.shape[0] / 128)))
    srcs = np.stack([srcs] * seq_len, axis=0)
    srcs = put_text_to_video_row(srcs, "Input Image", display_frame_nr=display_frame_nr)

    pred = ((pred.permute(0, 1, 3, 4, 2).cpu().numpy()) * 255).astype(np.uint8)[:n_logged]
    pred = np.concatenate(list(pred), axis=2)
    pred = put_text_to_video_row(pred, "Predicted Video", display_frame_nr=display_frame_nr)

    tgt = ((tgt.permute(0, 1, 3, 4, 2).cpu().numpy()) * 255).astype(np.uint8)[:n_logged]
    tgt = np.concatenate(list(tgt), axis=2)
    tgt = put_text_to_video_row(tgt, "Groundtruth Video", display_frame_nr=display_frame_nr)

    full = np.concatenate([srcs, pred, tgt, traj_vis], axis=1)
    if logwandb:
        full = np.moveaxis(full, [0, 1, 2, 3], [0, 2, 3, 1])

    return full
Exemplo n.º 17
0
    def _findContours(self):
        contours = []
        masks = self.masks.detach().numpy()
        use_cv2 = True
        for mask in masks:
            if use_cv2:
                mask = cv2.UMat(mask)
                contour, hierarchy = cv2_util.findContours(
                    mask,
                    cv2.RETR_EXTERNAL,
                    cv2.CHAIN_APPROX_SIMPLE  # cv2.CHAIN_APPROX_TC89_L1
                )

                reshaped_contour = []
                for entity in contour:
                    assert len(entity.shape) == 3
                    assert (entity.shape[1] == 1
                            ), "Hierarchical contours are not allowed"
                    reshaped_contour.append(entity.reshape(-1).tolist())
                contours.append(reshaped_contour)
            else:
                contour = skimage.measure.find_contours(mask.astype('uint8'),
                                                        level=0.5)
                reshaped_contour = []
                for entity in contour:
                    assert len(entity.shape) == 2
                    assert (entity.shape[1] == 2
                            ), f"wrong contour shape {entity.shape}"
                    entity = np.flip(entity, axis=1)
                    # make it close
                    if not np.all(entity[0, :] == entity[-1, :]):
                        entity = np.vstack([entity, entity[0, :]])
                    reshaped_contour.append(entity.reshape(-1).tolist())
                contours.append(reshaped_contour)
        return contours
Exemplo n.º 18
0
 def mask_tank(self):
     x = int(self.tank.x_px)
     y = int(self.tank.y_px)
     R = int(self.tank.r_px) + 1  # ??
     if self.threshType == cv2.THRESH_BINARY_INV:
         tank_mask = 255 * np.ones_like(self.frame)
         if self.GPU:
             tank_mask = cv2.UMat(tank_mask)
         cv2.circle(tank_mask, (x, y), R, (0, 0, 0), thickness=-1)
         self.frame = cv2.bitwise_or(self.frame, tank_mask)
     else:
         tank_mask = np.zeros_like(self.frame)
         if self.GPU:
             tank_mask = cv2.UMat(tank_mask)
         cv2.circle(tank_mask, (x, y), R, (255, 255, 255), thickness=-1)
         self.frame = cv2.bitwise_and(self.frame, tank_mask)
Exemplo n.º 19
0
def run_opencv(b, **kwargs):
    # see https://www.learnopencv.com/opencv-transparent-api/
    time_upload = kwargs.get('time_upload', False)
    transparent_api = kwargs.get('transparent_api', False)
    B = kwargs.get('batch_size', 10)

    b = cv2.UMat(b) if (transparent_api and not time_upload) else b

    def run_once():
        x = cv2.UMat(b) if (transparent_api and time_upload) else b
        y = cv2.cvtColor(x, cv2.COLOR_BAYER_BG2RGB)
        return y

    run_once()
    run_once()
    y = run_once()
    if transparent_api:
        z = y.get()

    N = 1000 * B
    start = time.time()
    for _ in range(N):
        y = run_once()
    if transparent_api:
        z = y.get()
    return (time.time() - start) / N * 1000
Exemplo n.º 20
0
 def read(self):
     # return the frame most recently read
     if (self.tapi):
         # return OpenCV Transparent API UMat frame for H/W acceleration
         return (self.grabbed, cv2.UMat(self.frame))
     # return standard numpy frame
     return (self.grabbed, self.frame)
Exemplo n.º 21
0
def find_address(crop_gray, crop_org):
        template = cv2.UMat(cv2.imread('address_mask_%s.jpg'%pixel_x, 0))
        # showimg(template)
        #showimg(crop_gray)
        w, h = cv2.UMat.get(template).shape[::-1]
        #t1 = round(time.time()*1000)
        res = cv2.matchTemplate(crop_gray, template, cv2.TM_CCOEFF_NORMED)
        #t2 = round(time.time()*1000)
        #print 'time:%s'%(t2-t1)
        min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
        top_left = (max_loc[0] + w, max_loc[1] - int(20*x))
        bottom_right = (top_left[0] + int(1700*x), top_left[1] + int(550*x))
        result = cv2.UMat.get(crop_org)[top_left[1]-10:bottom_right[1], top_left[0]-10:bottom_right[0]]
        cv2.rectangle(crop_gray, top_left, bottom_right, 255, 2)
        #showimg(crop_gray)
        return cv2.UMat(result)
Exemplo n.º 22
0
 def process_gpu3(self, img, sub_factor=1.0):
     # Takes 1 combined numpy (Mat) array and converts to UMat on the fly
     # Converts combined image to UMat on gpu then transparent interface produces 2 sub image UMats in OPENCL
     # OPENCV transparent interface will use OPENCL for processing, uses scaleAdd routine for subtraction step
     # Approx 2.7 ms processing time on macbook pro (i7 + Intel Iris Pro)
     uimg = cv2.UMat(img)
     img_l = cv2.UMat(uimg, (0, self.height), (0, self.width))
     img_r = cv2.UMat(uimg, (0, self.height), (self.width, 2 * self.width))
     result = cv2.scaleAdd(
         cv2.remap(img_r,
                   self.defX,
                   self.defY,
                   cv2.INTER_LINEAR,
                   borderMode=cv2.BORDER_CONSTANT,
                   borderValue=0), -sub_factor, img_l)
     return result
def drawDetectorStatus(image):
    # obstacle notification notify
    custom_org = (200, 200)
    custom_fontscale = 2
    custom_color = (0, 150, 250) # orange
    custom_thickness = 2

    image = cv2.UMat(image)

    # draw side detect
    # front
    custom_fontscale = 2
    if detector_notify["front"] != 0 or detector_notify["back"] != 0 or detector_notify["left"] != 0 or detector_notify["right"] != 0:
        
        image = cv2.putText(image, f"WARNING", (50, 450), GLOBAL_FONT, 2, custom_color, custom_thickness)
        image = cv2.putText(image, f"obstacle nearby", (50, 480), GLOBAL_FONT, 1, custom_color, custom_thickness)
    
    custom_fontscale = 0.8
    image = cv2.putText(image, f"Obstacle detector", (50, 560), GLOBAL_FONT, custom_fontscale, custom_color, custom_thickness)
    image = cv2.putText(image, f"front: {detector_notify['front']} m", (50, 590), GLOBAL_FONT, custom_fontscale, custom_color, custom_thickness)
    image = cv2.putText(image, f"back: {detector_notify['back']} m", (50, 620), GLOBAL_FONT, custom_fontscale, custom_color, custom_thickness)
    image = cv2.putText(image, f"left: {detector_notify['left']} m", (50, 650), GLOBAL_FONT, custom_fontscale, custom_color, custom_thickness)
    image = cv2.putText(image, f"right: {detector_notify['right']} m", (50, 680), GLOBAL_FONT, custom_fontscale, custom_color, custom_thickness)

    detector_notify["front"] = 0
    detector_notify["back"] = 0
    detector_notify["left"] = 0
    detector_notify["right"] = 0

    return image
Exemplo n.º 24
0
    def prepare_image(self, processor):
        screen_matrix = ImageGrab.grab(bbox=(0, 60, 960, 560))
        screen = np.array(screen_matrix)

        if processor == GPU:
            screen = cv2.UMat(screen)
        return screen
Exemplo n.º 25
0
    def generate_saliency(self, src):

        # Convert pixels to 32-bit floats
        src = src.astype(np.float32)

        src_dimensions = src.shape[:2]

        # Use T-API for hardware acceleration
        src = cv2.UMat(src)

        if self.ch_3:

            # Split colour image into RBG channels
            channel_array = cv2.split(src)

            # Generate Saliency Map for each channel
            for channel in range(3):

                channel_array[channel] = self.divog_saliency(
                    channel_array[channel], src_dimensions)

            # Merge back into one grayscale image
            merged_channels = cv2.merge(channel_array)
            gray_merged_channels = cv2.cvtColor(merged_channels,
                                                cv2.COLOR_BGR2GRAY)

            return gray_merged_channels

        else:

            # Convert to grayscale
            src_bw = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)

            # Generate Saliency Map
            return self.divog_saliency(src_bw, src_dimensions)
Exemplo n.º 26
0
def watch_feed():
	# Create another socket - UDP for live feed
	udp_soc = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
	udp_soc.bind((Globals.target,Globals.LIVEFEED_PORT))
	
	FRAME_SIZE, addr = udp_soc.recvfrom(4096)
	print addr, FRAME_SIZE
	FRAME_SIZE = int(FRAME_SIZE)
	
	while True:
		# Capture frame-by-frame
		frame = ''
		while len(frame) < FRAME_SIZE:
			frame += udp_soc.recv(Globals.UDP_BUFFER_SIZE)#cap.read()
			#print frame
		
		# Display the resulting frame
		frame = numpy.fromstring(frame, dtype='uint8')
		frame = numpy.reshape(frame[:1280*720], (720, 1280))
		#frame = cv2.imdecode(frame,cv2.IMREAD_COLOR )
		frame_mat = cv2.UMat(frame)
		cv2.imshow('LIVE FEED', frame_mat)

		# Press Q on keyboard to  exit
		if cv2.waitKey(25) & 0xFF == ord('q'):
			break
			# Break the loop
	cv2.destroyAllWindows()
	udp_soc.close()
Exemplo n.º 27
0
def get_carla_status(vehicle_actor, image):
    # get carla status
    # original spawn point = x=-30, y=207.5, z=1
    spawnpt = [-30, 207.5, 1]

    vehicle_tf = vehicle_actor.get_transform()
    vehicle_loc = [
        round(vehicle_tf.location.x - spawnpt[0], 2),
        round(vehicle_tf.location.y - spawnpt[1], 2),
        round(vehicle_tf.location.z, 2)
    ]

    # display
    custom_org = (35, 50)
    custom_fontscale = 0.7
    custom_color = (255, 150, 120)  # blue
    custom_thickness = 2

    image = cv2.UMat(image)
    res = cv2.putText(
        image,
        f"location:(x={vehicle_loc[0]},y={vehicle_loc[1]},z={vehicle_loc[2]})",
        custom_org, GLOBAL_FONT, custom_fontscale, custom_color,
        custom_thickness)

    return res
Exemplo n.º 28
0
 def __init__(self,
              shape,
              dtype=float64,
              buffer=None,
              offset=0,
              strides=None,
              order=None,
              mat=None,
              UMat=False,
              usageFlags=USAGE_DEFAULT):
     self.UMat = UMat
     if mat is None:
         mat = np.ndarray(shape,
                          dtype=dtype,
                          buffer=buffer,
                          offset=offset,
                          strides=strides,
                          order=order)
     self.shape = mat.shape
     self.dtype = mat.dtype
     if UMat:
         self._n = None
         self.u = cv.UMat(mat)
         self._ = self.u
     else:
         self._n = mat
         self.u = None
         self._ = self._n
Exemplo n.º 29
0
    def __init__(self, source, qsize=128):
        """
            Source must be a path to a video file
            Utilizes your system's GPU to process the stream
        """

        if not isinstance(source, str):
            raise ValueError(
                f'Expected either a filepath. Got {type(source)}. Consider using VideoStream which supports both live video as well as pre-existing videos'
            )

        # initialize the file video stream along with the boolean
        # used to indicate if the thread should be stopped or not
        self.stream = cv.VideoCapture(source)
        self.kill_stream = False
        self.count = 0

        # initialize the queue to store frames
        self.Q = Queue(maxsize=qsize)

        self.width = int(self.stream.get(cv.CAP_PROP_FRAME_WIDTH))
        self.height = int(self.stream.get(cv.CAP_PROP_FRAME_HEIGHT))
        self.res = (self.width, self.height)

        self.fps = math.ceil(self.stream.get(FPS))
        self.frames = int(self.stream.get(FRAME_COUNT))

        # since we use UMat to store the images to
        # we need to initialize them beforehand
        self.qframes = [0] * qsize
        for ii in range(qsize):
            self.qframes[ii] = cv.UMat(self.height, self.width, cv.CV_8UC3)
def visualize_fmap(fmap, window_name='fmap'):
    # Convert to opencv image
    fmap_img = fmap.cpu().numpy().astype(np.float32)
    square_size = int(math.sqrt(fmap_img.shape[2]))
    index = 0
    channel_w, channel_h, channel_depth = fmap_img.shape

    fmap_img_draw = np.zeros((square_size * channel_w, square_size * channel_h, 3))

    channel_img_rgb = np.zeros((channel_w, channel_h, 3), dtype="uint8")
    for i in range(square_size):
        for j in range(square_size):
            # Get channel
            channel_img = cv2.UMat(fmap_img[:, :, index]).get()  # Found on github due to errors

            # channel_img = cv2.normalize(fmap_img[:, :, index], None, 255, 0, cv2.NORM_MINMAX, cv2.CV_8UC1)
            channel_img_rgb[:, :, 0] = channel_img
            channel_img_rgb[:, :, 1] = channel_img
            channel_img_rgb[:, :, 2] = channel_img

            # Include channel in image to draw
            fmap_img_draw[int(i * channel_w):int(i * channel_w + channel_w),
                            int(j * channel_h):int(j * channel_h + channel_h)] = channel_img_rgb

            # Increase index
            index += 1

        # Show image
        cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
        cv2.resizeWindow(window_name, 500, 500)
        cv2.imshow(window_name, fmap_img_draw)