简体   繁体   English

裁剪矩形并显示在 OpenCV Python 中裁剪的图像

[英]Crop rectangle & show image that cropped in OpenCV Python

I'm new on opencv, I have an original image like this我在OpenCV的新的,我有这样一个原始图像

and I want to crop each licence plate that i've cropped it with this code.我想用这个代码裁剪我裁剪过的每个车牌。

import re
import cv2
import numpy as np

# import tensorflow.lite as tflite
import tflite_runtime.interpreter as tflite # <-- import library

from PIL import Image


def load_labels(label_path):
    with open(label_path) as f:
        labels = {}
        for line in f.readlines():
            m = re.match(r"(\d+)\s+(\w+)", line.strip())
            labels[int(m.group(1))] = m.group(2)
        return labels


def load_model(model_path):
    # interpreter = tf.lite.Interpreter(model_path=args.model_file)
    interpreter = tflite.Interpreter(model_path=model_path)
    interpreter.allocate_tensors()
    return interpreter


def process_image(interpreter, image, input_index):
    input_data = np.expand_dims(image, axis=0)  # expand to 4-dim

    interpreter.set_tensor(input_index, input_data)
    interpreter.invoke()

    output_details = interpreter.get_output_details()
    # print(output_details)
    # output_details[0] - position
    # output_details[1] - class id
    # output_details[2] - score
    # output_details[3] - count

    positions = np.squeeze(interpreter.get_tensor(output_details[0]['index']))
    classes = np.squeeze(interpreter.get_tensor(output_details[1]['index']))
    scores = np.squeeze(interpreter.get_tensor(output_details[2]['index']))
    #confidence = np.squeeze(interpreter.get_tensor(output_details[3]['index']))

    result = []

    for idx, score in enumerate(scores):
        if score > 0.05:
            result.append({'pos': positions[idx], '_id': classes[idx] })
            print(score * 100)

    return result


def display_result(result, frame, labels):

    font = cv2.FONT_HERSHEY_SIMPLEX # <-- font
    size = 0.6
    color = (0, 255, 0)  # warna frame biru
    #color = (255, 131, 23)  # warna oren
    thickness = 1

    # position = [ymin, xmin, ymax, xmax]
    # x * IMAGE_WIDTH
    # y * IMAGE_HEIGHT
    width = frame.shape[1]
    height = frame.shape[0]

    for obj in result:
        pos = obj['pos']
        _id = obj['_id']

        x1 = int(pos[1] * width)
        x2 = int(pos[3] * width)
        y1 = int(pos[0] * height)
        y2 = int(pos[2] * height)

        cv2.putText(frame, labels[_id], (x1, y1), font, size, color, thickness) # <-- munculin font label
        cv2.rectangle(frame, (x1, y1), (x2, y2), color, thickness)

    cv2.imshow('test detect plat-nomor 1', frame)


if __name__ == "__main__":

    #model_path = '/home/smartron01/Documents/tflite/data/coco_ssd_mobilenet_v1_1.0_quant_2018_06_29/detect.tflite'
    model_path = '/home/smartron01/Documents/tflite/numberplate_dr_pak_hermawan/tflite/model.tflite'
    #label_path = '/home/smartron01/Documents/tflite/data/coco_labels.txt'
    label_path = '/home/smartron01/Documents/tflite/numberplate_dr_pak_hermawan/tflite/dict.txt'
    #image_path = '/home/smartron01/Documents/tflite/data/bus.jpg'
    #image_path = '/home/smartron01/Documents/tflite/data/0008A.jpg'
    image_path = '/home/smartron01/Documents/tflite/data/mobil2.jpg'

    interpreter = load_model(model_path)
    labels = load_labels(label_path)

    input_details = interpreter.get_input_details()

    input_shape = input_details[0]['shape']
    height = input_shape[1]
    width = input_shape[2]

    input_index = input_details[0]['index']

    frame = cv2.imread(image_path, cv2.IMREAD_COLOR)
    print(frame.shape) # print model shape

    image = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
    image = image.resize((width, height))

    top_result = process_image(interpreter, image, input_index)
    display_result(top_result, frame, labels)

    key = cv2.waitKey(0)
    if key == 27:
        cv2.destroyAllWindows()

I created a rectangle in each of license plate and this code gives me this ouput .我在每个车牌中创建了一个矩形,这段代码给了我这个输出 I want to crop each license-plate in each car, crop it rectangle, and then show image in another window.我想裁剪每辆车中的每个车牌,裁剪矩形,然后在另一个窗口中显示图像。 thankyou.谢谢你。

Hello I'm new to answering questions, but I believe this may help.您好,我是回答问题的新手,但我相信这可能会有所帮助。

I did something similar to this, but it crops faces from images.我做了类似的事情,但它从图像中裁剪了人脸。

You can apply the same concept in your code that draws the rectangles around the license plates to crop the images.您可以在代码中应用相同的概念,在车牌周围绘制矩形以裁剪图像。 The extra code goes with your display_result function.额外的代码与您的 display_result 函数一起使用。

for obj in result:
        pos = obj['pos']
        _id = obj['_id']

        x1 = int(pos[1] * width)
        x2 = int(pos[3] * width)
        y1 = int(pos[0] * height)
        y2 = int(pos[2] * height)

        cv2.putText(frame, labels[_id], (x1, y1), font, size, color, thickness) # <-- munculin font label
        cv2.rectangle(frame, (x1, y1), (x2, y2), color, thickness)
        #crops plates based on points used to draw each rectangle around plates.
        frame = frame[y1:y2,x1:x2]

    cv2.imshow('test detect plat-nomor 1', frame)

I hope this helps!我希望这有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM