裁剪矩形并显示在 OpenCV 中裁剪的图像 Python
Crop rectangle & show image that cropped in OpenCV Python
我是 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()
我在每个车牌中创建了一个矩形,此代码为我提供了 this ouput。我想裁剪每辆车的每个牌照,将其裁剪为矩形,然后在另一个 window 中显示图像。谢谢。
您好,我是回答问题的新手,但我相信这可能会有所帮助。
我做了类似的事情,但它从图像中裁剪了人脸。
您可以在代码中应用相同的概念,在车牌周围绘制矩形以裁剪图像。额外的代码与您的 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)
希望对您有所帮助!
我是 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()
我在每个车牌中创建了一个矩形,此代码为我提供了 this ouput。我想裁剪每辆车的每个牌照,将其裁剪为矩形,然后在另一个 window 中显示图像。谢谢。
您好,我是回答问题的新手,但我相信这可能会有所帮助。
我做了类似的事情,但它从图像中裁剪了人脸。
您可以在代码中应用相同的概念,在车牌周围绘制矩形以裁剪图像。额外的代码与您的 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)
希望对您有所帮助!