Я новичок в opencv, у меня есть оригинальное изображение, например this
, и я хочу обрезать каждый номерной знак, который я обрезал, с помощью этого кода.
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()
Я создал прямоугольник в каждом номерном знаке, и этот код дает мне этот вывод . Я хочу обрезать каждый номерной знак в каждой машине, обрезать его прямоугольником, а затем показать изображение в другом окне. большое спасибо.