import hyperlpr
import cv2
import numpy as np
from PIL import ImageFont, ImageDraw
from PIL import Image
def cv2AddChineseText(img, text, position, textColor=(0, 255, 0), textSize=30):
if (isinstance(img, np.ndarray)):
img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
draw = ImageDraw.Draw(img)
fontStyle = ImageFont.truetype(
"simsun.ttc", textSize, encoding="utf-8")
draw.text(position, text, textColor, font=fontStyle)
return cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR)
def main():
mp4 = cv2.VideoCapture("2.mp4")
while (mp4.isOpened()):
ret, frame = mp4.read()
if ret == True:
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
k = hyperlpr.HyperLPR_plate_recognition(frame)
print(type(k))
if k:
d = []
a = k[0][0]
b = k[0][1]
c = k[0][2]
print(a)
print(b)
b = str(b)
print(c)
d.append(c)
f = np.array(d, dtype=int)
print(type(d))
for (x, y, w, h) in d:
cv2.rectangle(frame, (x, y), (w, h), (0, 0, 255), 2)
frame = cv2AddChineseText(frame, a, (x, y), (255, 0, 0), 30)
cv2.putText(frame, b, (x, y), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
mp4.release()
cv2.destroyAllWindows()
if __name__ == '__main__':
main()