def draw_box_on_image(num_hands_detect, score_thresh, scores, boxes, classes, im_width, im_height, image_np,Line_Position2,Orientation):
    # Determined using a piece of paper of known length, code can be found in distance to camera
    focalLength = 875
    # The average width of a human hand (inches) http://www.theaveragebody.com/average_hand_size.php
    # added an inch since thumb is not included
    avg_width = 4.0
    # To more easily differetiate distances and detected bboxes

    global a,b
    hand_cnt=0
    color = None
    color0 = (255,0,0)
    color1 = (0,50,255)
    for i in range(num_hands_detect):
        
        if (scores[i] > score_thresh):
        
            if classes[i] == 1: 
                id = 'hand'
                
            if classes[i] == 2:
                id ='gloved_hand'
                avg_width = 3.0 # To compensate bbox size change
                #b=1
            
            if i == 0: color = color0
            else: color = color1

            (left, right, top, bottom) = (boxes[i][1] * im_width, boxes[i][3] * im_width,
                                          boxes[i][0] * im_height, boxes[i][2] * im_height)
            p1 = (int(left), int(top))
            p2 = (int(right), int(bottom))

            dist = distance_to_camera(avg_width, focalLength, int(right-left))
            
            if dist:
                hand_cnt=hand_cnt+1           
            cv2.rectangle(image_np, p1, p2, color , 3, 1)
            

            cv2.putText(image_np, 'hand '+str(i)+': '+id, (int(left), int(top)-5),
                        cv2.FONT_HERSHEY_SIMPLEX, 0.5 , color, 2)

            cv2.putText(image_np, 'confidence: '+str("{0:.2f}".format(scores[i])),
                        (int(left),int(top)-20),
                        cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0,255,0), 2)

            cv2.putText(image_np, 'distance from camera: '+str("{0:.2f}".format(dist)+' inches'),
                        (int(im_width*0.65),int(im_height*0.9+30*i)),
                        cv2.FONT_HERSHEY_SIMPLEX, 0.3, color, 2)
           
            a=alertcheck.drawboxtosafeline(image_np,p1,p2,Line_Position2,Orientation)
        if hand_cnt==0 :
            b=0
            #print(" no hand")
        else:
            b=1
            #print(" hand")        
            
    return a,b
Esempio n. 2
0
def draw_box_on_image(results, im_width, im_height, image_np, Line_Position2, Orientation):
    # Determined using a piece of paper of known length, code can be found in distance to camera
    focalLength = 875
    # The average width of a human hand (inches) http://www.theaveragebody.com/average_hand_size.php
    # added an inch since thumb is not included
    avg_width = 4.0
    # To more easily differetiate distances and detected bboxes

    global a,b
    hand_cnt=0
    color = None
    color0 = (255,0,0)
    color1 = (0,50,255)
   
    for r in results:
      
        # extract the bounding box and box and predicted class label
        box = r.bounding_box.flatten().astype("int")
        (startX, startY, endX, endY) = box
        label = labels[r.label_id]

        # draw the bounding box and label on the image
        cv2.rectangle(image_np, (startX, startY), (endX, endY),
                      (0, 255, 0), 1)
        y = startY - 15 if startY - 15 > 15 else startY + 15
        text = "{}: {:.2f}%".format(label, r.score * 100)
       

        cv2.putText(image_np, text, (startX, startY), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0))
        (left, right, top, bottom) = (box[0] , box[2] ,box[1] , box[3] )
                                      
        p1 = (int(left), int(top))
        p2 = (int(right), int(bottom))
        
        dist = distance_to_camera(avg_width, focalLength, int(right - left))
        if dist:
            hand_cnt = hand_cnt + 1
        a = alertcheck.drawboxtosafeline(image_np, p1, p2, Line_Position2, Orientation)
        if hand_cnt==0:
            b=0
        else:
            b=1

    return a,b
Esempio n. 3
0
def draw_box_on_image(num_cars_detect, score_thresh, scores, boxes, classes,
                      im_width, im_height, image_np):

    global a, b
    car_cnt = 0
    color0 = (255, 0, 0)
    color1 = (0, 50, 255)

    centeriX = tuple()
    centeriY = tuple()

    for i in range(num_cars_detect):

        if (scores[i] > score_thresh):

            if classes[i] == 1:
                id = 'Car'

            if i == 0:
                color = color0

                (left, right, top,
                 bottom) = (boxes[i][1] * im_width, boxes[i][3] * im_width,
                            boxes[i][0] * im_height, boxes[i][2] * im_height)
                p1 = (int(left), int(top))
                p2 = (int(right), int(bottom))

                cv2.rectangle(image_np, p1, p2, color, 3, 1)

                cv2.putText(image_np, 'Car ' + str(i) + ': ' + id,
                            (int(left), int(top) - 5),
                            cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)

                cv2.putText(image_np,
                            'confidence: ' + str("{0:.2f}".format(scores[i])),
                            (int(left), int(top) - 20),
                            cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
                (left, right, top,
                 bottom) = (boxes[i][1] * im_width, boxes[i][3] * im_width,
                            boxes[i][0] * im_height, boxes[i][2] * im_height)
                centeriX = (left / 2, bottom / 2)

            else:
                color = color1
                (left1, right1, top1,
                 bottom1) = (boxes[i][1] * im_width, boxes[i][3] * im_width,
                             boxes[i][0] * im_height, boxes[i][2] * im_height)
                p3 = (int(left1), int(top1))
                p4 = (int(right1), int(bottom1))

                cv2.rectangle(image_np, p3, p4, color, 3, 1)

                cv2.putText(image_np, 'Car ' + str(i) + ': ' + id,
                            (int(left1), int(top1) - 5),
                            cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)

                cv2.putText(image_np,
                            'confidence: ' + str("{0:.2f}".format(scores[i])),
                            (int(left1), int(top1) - 20),
                            cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)

                centeriY = (left1 / 2, bottom1 / 2)

    a = alertcheck.drawboxtosafeline(centeriX, centeriY)

    if car_cnt == 0:
        b = 0
    else:
        b = 1
    return a, b
def draw_box_on_image(num_hands_detect, score_thresh, scores, boxes, classes,
                      im_width, im_height, image_np, Line_Position2,
                      Orientation):
    # Determined using a piece of paper of known length, code can be found in distance to camera
    focalLength = 875
    # The average width of a human hand (inches) http://www.theaveragebody.com/average_hand_size.php
    # added an inch since thumb is not included
    avg_width = 4.0
    # To more easily differetiate distances and detected bboxes

    global a, b
    hand_cnt = 0
    color = None
    color0 = (255, 0, 0)
    color1 = (0, 50, 255)
    for i in range(num_hands_detect):

        if (scores[i] > score_thresh):

            #no_of_times_hands_detected+=1
            #b=b+1
            #b=1
            #print(b)
            if classes[i] == 1:
                id = 'HAND'
                #b=1

            if classes[i] == 2:
                id = 'CLOSED HAND'
                avg_width = 3.0  # To compensate bbox size change
                #b=1

            if i == 0: color = color0
            else: color = color1

            (left, right, top,
             bottom) = (boxes[i][1] * im_width, boxes[i][3] * im_width,
                        boxes[i][0] * im_height, boxes[i][2] * im_height)
            p1 = (int(left), int(top))
            p2 = (int(right), int(bottom))

            def is_close(p1, p2):
                """
                # 1. Calculate Euclidean Distance of two points
                :param:
                p1, p2 = two points for calculating Euclidean Distance

                :return:
                dst = Euclidean Distance between two 2d points
                """
                dst = math.sqrt(p1 + p2)
                return dst

            def convertBack(x, y, w, h):
                """
                # 2. Converts center coordinates to rectangle coordinates
                :param:
                x, y = midpoint of bounding box
                w, h = width, height of the bounding box

                :return:
                xmin, ymin, xmax, ymax
                """
                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

            if len(detections) > 0:
                centroid_dict = dict()
                objectId = 0
                for detection in detections:
                    name_tag = str(detection[0].decode())
                    if name_tag == 'person':
                        x, y, w, h = detection[2][0], \
                                     detection[2][1], \
                                     detection[2][2], \
                                     detection[2][3]
                    xmin, ymin, xmax, ymax = convertBack(
                        float(x), float(y), float(w), float(h))
                    centroid_dict[objectId] = (int(x), int(y), xmin, ymin,
                                               xmax, ymax)
                    objectId += 1
            cv2.line(image_np, (tlblX, tlblY), (trbrX, trbrY),
                     color=(255, 0, 0),
                     thickness=2,
                     lineType=8,
                     shift=0)
            dist = distance_to_camera(avg_width, focalLength,
                                      int(right - left))

            if dist:
                hand_cnt = hand_cnt + 1
            cv2.rectangle(image_np, p1, p2, color, 3, 1)

            cv2.putText(image_np, 'hand ' + str(i) + ': ' + id,
                        (int(left), int(top) - 5), cv2.FONT_HERSHEY_SIMPLEX,
                        0.5, color, 2)

            cv2.putText(image_np,
                        'confidence: ' + str("{0:.2f}".format(scores[i])),
                        (int(left), int(top) - 20), cv2.FONT_HERSHEY_SIMPLEX,
                        0.5, (0, 255, 0), 2)

            cv2.putText(
                image_np, 'distance from camera: ' +
                str("{0:.2f}".format(dist) + ' inches'),
                (int(im_width * 0.65), int(im_height * 0.9 + 30 * i)),
                cv2.FONT_HERSHEY_SIMPLEX, 0.3, color, 2)

            a = alertcheck.drawboxtosafeline(image_np, p1, p2, Line_Position2,
                                             Orientation)
        if hand_cnt == 0:
            b = 0
            #print(" no hand")
        else:
            b = 1
            #print(" hand")

    return a, b