def detect_common_objects(image):
    """Detects common objects"""

    bbox, label, conf = cv.detect_common_objects(image, confidence=0.25, model="yolov4-tiny")
    output_image = draw_bbox(image, bbox, label, conf)

    return output_image
예제 #2
0
def analyzeHike():
    global masterCounter, start_time

    for i in range(MAX_INDEX + 1):

        imagePath = dataPath + str(hikeID) + "/" + str(i) + "_cam2r.jpg"
        im = cv2.imread(imagePath)
        bbox, label, conf = cv.detect_common_objects(im)

        print("# image {}: \n\t{} \n\t{}".format(i, label, conf))

        res = []
        for j in range(len(label)):
            if (conf[j] > CONF_THRESHOLD):
                res.append(label[j])

        tmpCounter = Counter(res)
        masterCounter += tmpCounter

        print("### Filtered: {}".format(tmpCounter))
        print("### Updated Master: {}".format(masterCounter))

        # if (i > 5):
        #     exit()

    print("--- %s seconds ---" % (time.time() - start_time))
def check_for_objects(image_path):
    # load image with OpenCV
    image = cv2.imread(image_path)
    # get dimensions of original image
    og_height, og_width, rgb = image.shape
    total_widths = 0
    total_heights = 0
    # with computer vision, identify objects and return the bounding box co-ordinates, corrensponding labels and confidence scores
    coordinates, item, confidence = cvlib.detect_common_objects(image)
    if coordinates and coordinates[0]:
        # save coordinates of object
        for coordinate in coordinates:
            # unpacking coordinates of each object found
            x1, y1, x2, y2 = coordinate
            total_widths += (x2 - x1)
            total_heights += (y2 - y1)
        # calculate area to see if the objects occupy at least 50% of the image
        if (total_widths * total_heights) / (og_width * og_height) > 0.5:
            # add rectangles around the objects to the image with the axes, readjust rgb color back to original since OpenCV reads image colors differently
            output_image = cv2.cvtColor(
                draw_bbox(image, coordinates, item, confidence),
                cv2.COLOR_BGR2RGB)
            pyplot.imshow(output_image)
            return True
    return False
예제 #4
0
def obj_detect(img):
    """Function to proccess an image through cvlib API.

    Args:
        img (numpy.ndarray) = Array of image to proccess.

    Return:
        Dictionary of results.
    """
    name = str(uuid4())
    result = {}
    # Apply object detection
    bbox, labels, conf = cv.detect_common_objects(img, confidence=0.40)
    # Create results dictionary.
    for element in labels:
        result[element] = labels.count(element)
    # Draw bounding box over detected objects
    out = draw_bbox(img, bbox, labels, conf)
    img_data = base64.b64encode(out)
    #_, img_encoded = cv2.imencode('.jpg', out)
    #result['img'] = img_data
    try:
        os.mkdir('image/')
    except FileExistsError as error:
        pass
    # Save output
    archive_name = "image/" + name + ".jpg"
    cv2.imwrite(archive_name, out)
    # Function to save img in blob storage
    send_to_blob()
    return(result)
예제 #5
0
def detect_objects(image):
    im = cv2.imread(image)
    bbox, label, conf = cv.detect_common_objects(im)
    output_image = draw_bbox(im, bbox, label, conf)
    output_image = cv2.cvtColor(output_image, cv2.COLOR_BGR2RGB)

    plt.imsave('{}/response.jpg'.format(image.split('/')[0]), output_image)
예제 #6
0
 def is_correct(self, obj, url, taskkey):
     while True:
         try:
             image = requests.get(url)
             nparr = np.frombuffer(image.content, np.uint8)
             im = cv2.imdecode(nparr, flags=1)
             objects = cv.detect_common_objects(im,
                                                confidence=0.5,
                                                nms_thresh=1,
                                                enable_gpu=False)[1]
             if obj.lower() in objects:
                 print(
                     f'{Fore.GREEN}[{Fore.WHITE}INFO{Fore.GREEN}] {Fore.CYAN}{taskkey}.jpg is a {obj}'
                 )
                 self.builder['answers'][taskkey] = 'true'
                 return
             print(
                 f'{Fore.GREEN}[{Fore.WHITE}INFO{Fore.GREEN}] {Fore.CYAN}{taskkey}.jpg is not a {obj}'
             )
             self.builder['answers'][taskkey] = 'false'
             break
         except Exception as e:
             print(
                 f"{Fore.RED}[{Fore.WHITE}ERROR{Fore.RED}] Unexpected error: {Fore.WHITE}{e}"
             )
def main():
    config.init()
    iteration = 0
    savedContentList = []

    for framename in os.listdir(config.input_dir):
        print("iteration: " + str(iteration))

        frame = cv2.imread(config.input_dir + "/" + framename)
        #collect infos
        frame, emotions, face_boxes = config.emotion_model.recognise_emotion(frame)
        person_boxes, env_boxes, person_labels, env_labels, confs = cvlib.detect_common_objects(frame, confidence=.75)
        frame = cvlib.object_detection.draw_person_bbox(frame, person_boxes, person_labels, confs, write_conf=False)
        frame = cvlib.object_detection.draw_env_bbox(frame, env_boxes, env_labels, confs, write_conf=False)
        words = txtConv.convert(frame)
        # create photoObject
        photo_object = po.PhotoObject(emotions, face_boxes, person_boxes, env_boxes, person_labels, env_labels, words, time.time())
        #check
        content_identifier = isValidContent(photo_object, savedContentList)

        if(content_identifier != "no_identifer"):
            cv2.imwrite(config.output_dir + '%s_%s.jpeg' % (iteration, content_identifier), frame)
            row_content = [framename[4:5],iteration,content_identifier,time.strftime('%H:%M:%S', helper.getLocalTime(photo_object.photo_time))]
            helper.save_to_csv('output/contents.csv', row_content)
            savedContentList.append(po.ContentToSave(content_identifier, photo_object))
            print("relevant picture saved.")
        
        iteration+=1
예제 #8
0
    def detect(self,
               frame: np.array,
               det_objs: set = None) -> (int, np.array, list):
        bbox, labels, conf = cv.detect_common_objects(
            frame,
            confidence=.5,
            model=self.model,
        )

        self.logger.info(labels)

        # only keep in the det_objs set
        if det_objs is not None:
            idxs = [i for i, l in enumerate(labels) if l in det_objs]
            labels = list(np.array(labels)[idxs])
            bbox = list(np.array(bbox)[idxs])
            conf = list(np.array(conf)[idxs])

        frame = draw_bbox(
            img=frame,
            bbox=bbox,
            labels=labels,
            confidence=conf,
            write_conf=False,
        )

        return 0, frame, labels
예제 #9
0
 def getCoordinatesMultiTargetLP(self, img, targets=[]):
     if len(targets) == 0:
         targets.append(self.target)
     bbox, label, conf = cv.detect_common_objects(
         img, model='yolov4-tiny')  #detects objects
     returnTargets = []  #creates the array for returning the objects
     if len(label) != 0:
         #xCo=None
         #yCo=None
         for i in range(0, len(label)):  #loops through the labels
             if label[
                     i] in self.targets:  #checks if the label is equal to one of the targets
                 x = bbox[i]
                 xCo = (x[0] + x[2]
                        ) / 2  #calculates the centre of the bounding box
                 yCo = (x[1] + x[3]) / 2
                 returnTargets.append(
                     [label[i], xCo,
                      yCo])  #appends the label and coordiates to the list
     #except:
     else:
         #xCo=None
         #yCo=None
         returnTargets = None  #sets the list to None
     if self.imageReturnQueue != None:
         warnings.warn('placing an image on the return Queue')
         self.imageReturnQueue.put({
             'img': img,
             'box': bbox,
             'label': label,
             'confidence': conf
         })  #returns the image
     else:
         warning.warn('no queue found')
     return returnTargets
예제 #10
0
def uploadPhoto(photo):
    im = cv2.imread(photo)
    bbox, label, conf = cv.detect_common_objects(im)
    output_image = draw_bbox(im, bbox, label, conf)
    print(label)
    addQuery(label)
    return output_image
def main():

    # instantiate video capture object
    cap = cv2.VideoCapture(0)
    # creating a queue to share data to speech process
    speakQ = multiprocessing.Queue()
    # creating speech process to not hang processor
    p1 = multiprocessing.Process(target=speak, args=(speakQ, ))
    # starting process 1 - speech
    p1.start()

    # keeps program running forever until ctrl+c or window is closed
    while True:
        # capture image
        _, img = cap.read()
        # use detect common objects model to label objects
        bbox, labels, conf = cv.detect_common_objects(img)
        # draw labels
        img = draw_bbox(img, bbox, labels, conf)
        # send unique string denoting new labels being sent to speech
        speakQ.put('#new#')
        # send each label to text to speech process
        for label in labels:
            speakQ.put(label.lower())
        # display and wait 10ms
        cv2.imshow('my webcam', img)
        cv2.waitKey(10)

    # clean up if you want to remove while loop
    cap.release()
    p1.terminate()
    cv2.destroyAllWindows()
예제 #12
0
def process(time):
    counts = {1: 29, 2: 26, 3: 75, 4: 51}
    for i in range(4):
        if int(time) <= lot_limit[i]:
            img_name = '../img/' + lot[i] + '_' + time + '.jpg'
            im = cv2.imread(img_name)
            bbox, label, conf = cv.detect_common_objects(im)
            output_image = draw_bbox(im,
                                     bbox,
                                     label,
                                     conf,
                                     colors=np.random.uniform(80,
                                                              81,
                                                              size=(80, 3)))
            # plt.savefig("../output_img/"+lot[i] + '_' + time + '.jpg')

            cv2.imwrite("../output_img/" + lot[i] + '_' + time + '.jpg',
                        output_image)
            # plt.subplots()

            # plt.imshow(output_image)
            # plt.show()
            count = label.count('car')
            counts[i + 1] -= count
        else:
            counts[i + 1] = -1
    return counts
예제 #13
0
def detect():
    path = os.getcwd()
    img = cv2.imread(path + "/pics/frame5.jpg")
    boxes, labels, _conf = cv.detect_common_objects(img, model="yolov3")

    print(labels)
    return labels
예제 #14
0
    def detect(self, image):
        npimg = np.fromstring(image, np.uint8)
        image = cv2.imdecode(npimg, cv2.IMREAD_COLOR)
        _, labels, _ = cv.detect_common_objects(image)

        if "cat" in labels:
            self.found = True
            self.missed_frame = MISSED_FRAME_COUNTER
            self.lost_timestamp = None

            if self.found_timestamp is None:
                self.found_timestamp = datetime.now()

            now = datetime.now()
            if self.last_time_saved is None or now > self.last_time_saved:
                self.last_time_saved = now + timedelta(minutes = 5)
                cv2.imwrite(os.path.join(os.getcwd(), 'cat', str(now.timestamp()) + ".jpg"), image)
                print("Cat found at: {}". format(now))

        elif self.found:
            if self.missed_frame > 0:
                self.missed_frame -= 1

                if self.lost_timestamp is None:
                    self.lost_timestamp = datetime.now()
            else:
                Thread(target=save_event, args=[self.found_timestamp, self.lost_timestamp]).start()
                self.found = False
                self.found_timestamp = None
예제 #15
0
def car_count(image_path):
    """

    Parameters
    ----------
    image_path : string
        expects a file path to an image in jpg format.

    Returns
    -------
    car_count: integer
        If the file path is valid, a count of the number of cars, trucks, and buses 
        in the image is returned.

    """
    try:
        # read in image
        im = cv2.imread(image_path)
        # detect objects in image
        bbox, label, conf = cv.detect_common_objects(im)
        # sum the number of trucks cars and buses
        car_count = label.count('truck') + label.count('car') + label.count(
            'bus')
    except:
        # if unable to read image return NA
        car_count = np.nan

    return car_count
예제 #16
0
def get_objects_of_interest(frame):
    """REQUIRED function.

    There are two options for getting a detector's results:
    Either get_confidence_score() OR get_objects_of_interest()

    get_confidence_score() must return a confidence level from 0.00 to 1.00.
    This represents how confident the detector is that
    something is out of the ordinary.

    get_objects_of_interest() returns a list of tuples, where each object
    is formatted like this:
    (geometry, object_name, object_confidence)
    """
    global _min_confidence
    if _min_confidence is None:  # Ensure _min_confidence != None
        logger.error("Unable to fetch _min_confidence from the database!")
        logger.error("_min_confidence was None!")
        logger.warning("Falling back to default confidence of 0.25.")
        _min_confidence = 0.25
    objects_of_interest = []
    geometry, label, conf = cv.detect_common_objects(
        frame, confidence=_min_confidence, model='yolov3-tiny')

    for i in range(len(label)):  # Iterate over every object
        object_tuple = (geometry[i], label[i], conf[i])
        objects_of_interest.append(object_tuple)
    # All objects should be in the list now...
    return objects_of_interest
예제 #17
0
    def detect_objects_basic(self, frame, mask):

        detected_objects = []

        print(frame.shape)

        # bbox, label, conf = detect_common_objects(frame, confidence=0.25)
        # bbox, label, conf = detect_common_objects(frame, confidence=0.75, enable_gpu=True)
        bbox, label, conf = detect_common_objects(frame,
                                                  confidence=0.3,
                                                  model='yolov3-tiny')

        if DEBUG_MODE:
            output_image = draw_bbox(frame, bbox, label, conf)
            cv2.imshow('all objects', output_image)

            # for i in range(len(bbox)):
            #     detected_objects.append({
            #         'label': label[i],
            #         'bbox': bbox[i],
            #         'conf': conf[i],
            #         'x': extracted_object['x'],
            #         'y': extracted_object['y'],
            #         'width': extracted_object['width'],
            #         'height': extracted_object['height'],
            #         'center_x': int(extracted_object['x'] + extracted_object['width'] / 2),
            #         'center_y': int(extracted_object['y'] + extracted_object['height'] / 2)
            #     })

            print(detected_objects)

        return detected_objects
def countVeh(imagePath):
    """
  TO COUNT NO OF VEHICHLES

  Using Tiny Yolo v3  Pre-Trained on Ms-Coco dataset
  
  Arguments:
      imagePath {[string]} -- [path of image]
  """

    image = cv2.imread(imagePath)
    #boundingbox, label, conf = cv.detect_common_objects(image)
    boundingbox, label, conf = cv.detect_common_objects(image,
                                                        confidence=0.25,
                                                        model='yolov3-tiny')
    output_image = draw_bbox(image, boundingbox, label, conf)

    plt.imshow(output_image)
    plt.show()
    print('Number of Objects in the image is ', "Cars :",
          str(label.count('car')), " Bikes :", str(label.count('motorcycle')),
          " Bicycles :", str(label.count('bicycle')), " Trucks :",
          str(label.count('truck')), "Bus :", str(label.count('bus')))

    countJson = {
        "Labels": {
            "Cars": label.count('car'),
            "Bikes": label.count('motorcycle'),
            "Bicycles": label.count('bicycle'),
            "Trucks": label.count('truck'),
            "Bus": label.count('bus')
        }
    }

    allImageCount[imagePath] = countJson
예제 #19
0
def live():
    loadvideo = cv2.VideoCapture('data/demo.mp4')

    if not loadvideo.isOpened():
        print("Could not open video")
        exit()

    while loadvideo.isOpened():
        status, frame = loadvideo.read()

        if not status:
            break

        bbox, label, conf = cv.detect_common_objects(frame,
                                                     confidence=0.25,
                                                     model='yolov3-tiny')

        print(bbox, label, conf)

        out = draw_bbox(frame, bbox, label, conf, write_conf=True)

        cv2.imshow("Real-time object detection", out)

        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    loadvideo.release()
    cv2.destroyAllWindows()
예제 #20
0
def obj_det():
    
    global obj_list,frame
    
    while not stop:
        
        resz_frame=cv2.resize(frame,(160,120))
        with tf.device("/CPU:0"):
            b,l,c = cv.detect_common_objects(resz_frame,enable_gpu=False,model='yolov3lite')
            ls1=[]
            for box,label,conf in zip(b,l,c):
                if label=='person':
                    x1=int(box[0]*d[0]/160)
                    y1=int(box[1]*d[1]/120)
                    x2=int(box[2]*d[0]/160)
                    y2=int(box[3]*d[1]/120)

                    x=int(x1+(x2-x1)/2)
                    y=int(y2+(y1-y2)/2)

                    color = list(np.random.random(size=3) * 256)
                    ls1.append((x,y))

            obj_list=ls1[:]
    return
예제 #21
0
def upload_to_aws():
    #we know for sure that the apple.jpg is in fact existent
    s3 = boto3.client('s3',
                      aws_access_key_id=ACCESS_KEY,
                      aws_secret_access_key=SECRET_KEY)
    bucketname = 'elasticbeanstalk-us-east-1-864793221722'
    keyname = 'input.jpg'
    tmp = tempfile.NamedTemporaryFile()
    with open(tmp.name, 'wb') as f:
        s3.download_fileobj(bucketname, keyname, f)

    im = cv2.imread(tmp.name)
    h, w, c = im.shape
    centerx = w / 2
    centery = h / 2
    bbox, label, conf = cv.detect_common_objects(im)

    finallist = [x for x in bbox if determine(x, centerx, centery)]

    if (len(finallist) > 0):
        left = finallist[0][0]
        top = finallist[0][1]
        right = finallist[0][2]
        bottom = finallist[0][3]
        im1 = im[top:bottom, left:right]
        cv2.imwrite("output.jpg", im1)
        s3.upload_file("output.jpg", bucketname, 'trial2.jpg')
    os.remove("output.jpg")
    return 'went fine'
예제 #22
0
def video_stream():
    # grab the frame from the threaded video stream
    frame = cap.read()
    frame = frame[1] if args.get("input", False) else frame
    #print(_)

    # convert the input frame from BGR to RGB then resize it to have
    # a width of 750px (to speedup processing)
    rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
    rgb = imutils.resize(frame, width=750)
    #plt.imshow(frame)
    #plt.show()

    #r = frame.shape[1] / float(rgb.shape[1])

    # detect the (x, y)-coordinates of the bounding boxes
    # corresponding to each face in the input frame, then compute
    # the facial embeddings for each face
    bbox, label, conf = cv.detect_common_objects(rgb, enable_gpu=True)
    frame = draw_bbox(rgb, bbox, label, conf)
    #cv2.line(frame, (0, 250),(750,250),(0, 0, 0))

    #are supposed to display the output frame to
    # the screen
    if args["display"] > 0:
        fram = frame
        cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
        img = Image.fromarray(cv2image)
        imgtk = ImageTk.PhotoImage(image=img)
        lmain.imgtk = imgtk
        lmain.configure(image=imgtk)
        lmain.after(1, video_stream)
예제 #23
0
def calibrate_camera():
    img = cv2.imread('images/calibration_30.jpg')
    bbox, label, conf = cv.detect_common_objects(img)
    width = bbox[label.index('orange')][2] - \
            bbox[label.index('orange')][0]
    width *= FACTOR_FROM_RESIZE
    focal_length = (width * KNOWN_DISTANCE) / KNOWN_SIZE
    return focal_length
예제 #24
0
def detect3():

    im = cv2.imread('image/cam3.jpeg')
    bbox, label, conf = cv.detect_common_objects(im)
    output_image = draw_bbox(im, bbox, label, conf)
    s3 = int(label.count('car'))
    s3d = s3
    print("number of vehicles", s3)
    calculation3(s3)
예제 #25
0
 def clicked(self):
     filename = askopenfilename()
     img = mpimg.imread(filename)
     imgplot = plt.imshow(img)
     im = cv2.imread(filename)
     bbox, label, conf = cv.detect_common_objects(im)
     output_image = draw_bbox(im, bbox, label, conf)
     plt.imshow(output_image)
     plt.show()
예제 #26
0
def detect4():

    im = cv2.imread('image/cam4.jpeg')
    bbox, label, conf = cv.detect_common_objects(im)
    output_image = draw_bbox(im, bbox, label, conf)
    s4 = int(label.count('car'))
    s4d = s4
    print("no of vehicles", s4)
    calculation4(s4d)
예제 #27
0
def detect():

    im = cv2.imread('image/cam1.jpeg')
    bbox, label, conf = cv.detect_common_objects(im)
    output_image = draw_bbox(im, bbox, label, conf)
    s=int(label.count('car'))
    s1d=s
    print("no of vehicles1 ",s)
    calculation1(s)
def countVehicleVideo(videoPath):
    """[summary]
      
  
  Arguments:
      videoPath {[string]} -- [VideoPath]
  """

    vidObj = cv2.VideoCapture(videoPath)

    count = 0

    success = 1

    while success:

        success, image = vidObj.read()

        boundingbox, label, conf = cv.detect_common_objects(
            image, confidence=0.20, model='yolov3-tiny')
        output_image = draw_bbox(image, boundingbox, label, conf)

        car = label.count('car')
        bike = label.count('motorcycle')
        bicycle = label.count('bicycle')
        truck = label.count('truck')
        bus = label.count('bus')

        if not ((bike == 0) and (car == 0) and (truck == 0) and (bus == 0) and
                (bicycle == 0)):

            print(len(label))
            plt.imshow(output_image)

            plt.show()
            print('Number of Objects in the image is ',
                  "Cars :", str(car), " Bikes :", str(bike), " Bicycles :",
                  str(bicycle), " Trucks :", str(truck), "Bus :", str(bus))

        countJson = {
            "Labels": {
                "Cars": car,
                "Bikes": bike,
                "Bicycles": bicycle,
                "Trucks": truck,
                "Bus": bus
            }
        }

        allImageCount["img{:d}".format(count)] = countJson

        count += 1

        #boundingbox, label, conf = cv.detect_common_objects(image)

    vidObj.release()
    cv2.destroyAllWindows()
예제 #29
0
def detect2():

    im = cv2.imread('image/cam2.jpeg')
    bbox, label, conf = cv.detect_common_objects(im)
    output_image = draw_bbox(im, bbox, label, conf)
    s2 = int(label.count('car'))
    s2d = s2
    print("no of vehicles2", s2)
    calculation2(s2)
예제 #30
0
def get_image_data(df):
    #df = pd.read_csv("data/data.csv")
    df = df.values
    freq_list = []
    sessions = {}
    for i in range(len(df)):
        X = df[i, 1]
        X = json.loads(X)
        Y = df[i, 2]
        Y = json.loads(Y)
        im = cv2.imread('data/images/image' + str(i) + '.png')

        dim = (1920, 1080)
        resized = cv2.resize(im, dim, interpolation=cv2.INTER_AREA)
        bbox, label, conf = cv.detect_common_objects(resized)
        freq = 0
        for j in range(len(label)):
            if label[j] != 'laptop' and label[j] != 'tv':
                x1, y1, x2, y2 = bbox[j]
                for k in range(len(X)):
                    if FindPoint(x1, y1, x2, y2, X[k], Y[k]):
                        freq += 1
        if len(X) != 0:
            freq_list.append(freq / len(X))
        else:
            freq_list.append(0)
        output_image = draw_bbox(resized, bbox, label, conf)
        if len(X) > 2:
            try:
                hmax = sns.kdeplot(X,
                                   Y,
                                   color='r',
                                   shade=True,
                                   cmap=alpha_cmap(plt.cm.viridis),
                                   shade_lowest=False)
                #hmax.collections[0].set_alpha(0)
                xmin = min([x for x in X if x > 0])
                xmax = max([x for x in X if x < 1980])
                ymin = min([x for x in Y if x > 0])
                ymax = max([x for x in Y if x < 1080])
                print(xmin, xmax, ymin, ymax)
                if df[i, 4] not in sessions:
                    sessions[df[i, 4]] = [(abs((ymax - ymin) * (xmax - xmin)),
                                           (im.shape[0] * im.shape[1]))]
                else:
                    sessions[df[i, 4]].append((abs(
                        (ymax - ymin) * (xmax - xmin)),
                                               (im.shape[0] * im.shape[1])))
            except:
                print("exception occured")

        plt.imshow(output_image, zorder=0)
        plt.savefig("heatmaps/map" + str(i) + ".png")
        plt.clf()

    return freq_list, sessions