Beispiel #1
0
def main():
    scores = [0]
    angle = 0
    iter = 1
    start = time.time()
    while((np.mean(scores[:3]) < 0.7) and (angle < 180) and (np.mean(scores[:2]) < 0.65) and iter <= 10):

        model = load_inference_model(MODEL_PATH)
        
        # load image
        image = read_image_bgr(IMAGE_PATH + IMAGE_NAME)
        
        #Rotate
        if(iter != 1):
            angle = angle + 20

        image = rotate(image,angle)

        # copy to draw on
        draw = image.copy()
        draw = cv2.cvtColor(draw, cv2.COLOR_BGR2RGB)

        #rotate
        
        
        # preprocess image for network
        image = preprocess_image(image)
        image, _ = resize_image(image, 416, 448)
        
        # process image
        #start = time.time()
        boxes, scores, labels = model.predict_on_batch(np.expand_dims(image, axis=0))
        print("iteration: ",str(iter))
        print("processing time: ", time.time() - start)
        
        boxes = post_process(boxes, draw, image)
        labels = labels[0]
        #print(labels)
        scores = scores[0]
        #print(scores)
        boxes = boxes[0]
        
        if(iter == 1):
            angle = -40
        iter = iter + 1

    #print(labels)
    #print(scores)    
    visualize_boxes(draw, boxes, labels, scores, class_labels=['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'])

    # 5. plot    
    plt.imshow(draw)
    plt.imsave(SAVE_PATH + IMAGE_NAME,draw)
    plt.show()
Beispiel #2
0
def processImage(image, index):

    Width = image.shape[1]
    Height = image.shape[0]
    print(Width)
    print(Height)

    #cv2.imshow('original frame',image)
    #cv2.waitKey(0)
    #cv2.destroyAllWindows()

    # read pre-trained model and config file
    net = cv2.dnn.readNet(WEIGHTS, CONFIG)

    # create input blob
    blob = cv2.dnn.blobFromImage(image,
                                 scale, (416, 416), (0, 0, 0),
                                 True,
                                 crop=False)
    #blob = cv2.dnn.blobFromImage(image, scale, (320,320), (0,0,0), True, crop=False)
    # set input blob for the network
    net.setInput(blob)

    # run inference through the network
    # and gather predictions from output layers
    outs = net.forward(get_output_layers(net))

    # initialization
    class_ids = []
    confidences = []
    boxes_b = []
    # for each detetion from each output layer
    # get the confidence, class id, bounding box params
    # and ignore weak detections (confidence < 0.5)
    for out in outs:
        for detection in out:
            scores = detection[5:]
            class_id = np.argmax(scores)
            confidence = scores[class_id]
            if confidence > 0.70:
                center_x = int(detection[0] * Width)
                center_y = int(detection[1] * Height)
                w = int(detection[2] * Width)
                h = int(detection[3] * Height)
                x = center_x - w / 2
                y = center_y - h / 2
                class_ids.append(class_id)
                confidences.append(float(confidence))
                boxes_b.append([x, y, w, h])

                # # do OCR in half upper part of the boxes
                # print("la parte donde esta mirando el testo de las imagenes");
                # cropped_img = image[y:y + h, x:x + w]  #--- Notice this part where you have to add the stride as well ---
                # cropped_img = cv2.cvtColor(cropped_img, cv2.COLOR_BGR2GRAY) #convert to grey scale
                # cropped_img= cv2.bilateralFilter(cropped_img, 11, 17, 17) #Blur to reduce noise
                # cv2.imshow('img',cropped_img)
                # custom_config = r'--psm 11'
                # text = pytesseract.image_to_string(cropped_img,lang="digits_added", config= custom_config)
                # print("Cropped:",text)
                # cv2.waitKey(0)
                # cv2.destroyAllWindows()

    # apply non-max suppression
    indices = cv2.dnn.NMSBoxes(boxes_b, confidences, conf_threshold,
                               nms_threshold)

    # go through the detections remaining
    # after nms and draw bounding box
    for i in indices:
        i = i[0]
        box = boxes_b[i]
        x = box[0]
        y = box[1]
        w = box[2]
        h = box[3]
        #print("boxes to be drawn");
        draw_bounding_box(image, class_ids[i], confidences[i], round(x),
                          round(y), round(x + w), round(y + h))
        # cv2.imshow("object_location_boxes", image)
        # # #wait until any key is pressed
        # cv2.waitKey()
        # cv2.destroyAllWindows()

        if (class_ids[i] == 5):
            print('bus located')
            # do OCR in half upper part of the boxes
            print("la parte donde esta mirando el testo de las imagenes")
            cropped_img = image[
                int(y):int(y) + int(h / 3),
                int(x) + int(1 * w / 10):int(x) + int(
                    7 * w / 10
                )]  #--- Notice this part where you have to add the stride as well ---
            #cropped_img = cv2.cvtColor(cropped_img, cv2.COLOR_BGR2GRAY) #convert to grey scale
            #cropped_img= cv2.bilateralFilter(cropped_img, 11, 17, 17) #Blur to reduce noise
            cv2.imshow('img', cropped_img)

            # copy to draw on
            draw = cropped_img.copy()
            draw = cv2.cvtColor(draw, cv2.COLOR_BGR2RGB)

            # # preprocess image for network
            cropped_img = preprocess_image(cropped_img)
            cropped_img, _ = resize_image(cropped_img, 416, 448)
            # #cropped_img, _ = resize_image(cropped_img, 410, 448)
            plt.imshow(cropped_img)
            #plt.imshow(_);

            # process image
            start = time.time()
            boxes_d, scores, labels = model.predict_on_batch(
                np.expand_dims(cropped_img, axis=0))
            print(labels)
            print("processing time: ", time.time() - start)

            boxes_d = post_process(boxes_d, draw, cropped_img)
            labels = labels[0]
            scores = scores[0]
            boxes_d = boxes_d[0]
            print(labels.shape)
            print(boxes_d.shape)
            print(scores[0])

            visualize_boxes(draw,
                            boxes_d,
                            labels,
                            scores,
                            class_labels=[
                                '0', '1', '2', '3', '4', '5', '6', '7', '8',
                                '9'
                            ])

            # 5. plot
            plt.imshow(draw)
            plt.show()

            digits_array = np.concatenate(
                (boxes_d, scores[:, np.newaxis], labels[:, np.newaxis]),
                axis=1)
            digits_array = digits_array[digits_array[:, 4] > 0.35]
            sortedDigits_score = digits_array[digits_array[:, 0].argsort()]
            #sortedDigits_left_right = digits_array[digits_array[:,0].argsort()]
            print(sortedDigits_score)
            ##### UNcomment when I solve the errors when it does not get three numbers and also when you solve the algorithm for knowing how many numbers there are.
            # print(sortedDigits_score.shape)
            # print("the number is")
            # print(str(int(sortedDigits_score[0][5])) + str(int(sortedDigits_score[1][5])) + str(int(sortedDigits_score[2][5])))

            # #grayscaled = cv2.cvtColor(cropped_img,cv2.COLOR_BGR2GRAY)
            # #brigthness = brigthness_level(grayscaled);
            # #print(brigthness);
            # #retval, th = cv2.threshold(grayscaled, brigthness*1.2, 255, cv2.THRESH_BINARY_INV)
            # #th = cv2.adaptiveThreshold(grayscaled, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 115, 1)
            # #retval2,th = cv2.threshold(grayscaled,254,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
            # #cv2.imshow('Adaptive threshold',th)
            # cv2.waitKey(0)
            # cv2.destroyAllWindows()
            # print("to print image to data");
            # print(pytesseract.image_to_data(Image.fromarray(cropped_img), config = '--psm 11',output_type='data.frame'))
            # custom_config = r'--psm 11'
            # text = pytesseract.image_to_string(cropped_img,lang="digits_added", config= custom_config)
            # print("Cropped:",text)
            cv2.waitKey(0)
            cv2.destroyAllWindows()
    # copy to draw on
    draw = image.copy()
    draw = cv2.cvtColor(draw, cv2.COLOR_BGR2RGB)

    # preprocess image for network
    image = preprocess_image(image)
    image, _ = resize_image(image, 416, 448)

    # process image
    start = time.time()
    boxes, scores, labels = model.predict_on_batch(
        np.expand_dims(image, axis=0))
    print("processing time: ", time.time() - start)

    boxes = post_process(boxes, draw, image)
    labels = labels[0]
    scores = scores[0]
    boxes = boxes[0]

    visualize_boxes(
        draw,
        boxes,
        labels,
        scores,
        class_labels=['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'])

    # 5. plot
    plt.imshow(draw)
    plt.show()