You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
45 lines
1.2 KiB
45 lines
1.2 KiB
import time
|
|
import cv2
|
|
from detect_ecnu import Detector
|
|
import numpy
|
|
from PIL import Image, ImageDraw, ImageFont
|
|
|
|
# read camera
|
|
cap = cv2.VideoCapture(0)
|
|
|
|
detector=Detector()
|
|
|
|
while (True):
|
|
# Capture frame-by-frame
|
|
ret, frame = cap.read()
|
|
cv2.imwrite("buffer.jpg",frame)
|
|
res=detector.detect(source='buffer.jpg')
|
|
|
|
for item in res:
|
|
if (item[0] == 'mask'):
|
|
color = (0, 255, 255)
|
|
rbg=(255, 255, 0)
|
|
text='普通口罩'
|
|
else:
|
|
color = (0, 255, 0)
|
|
rbg=(0,255,0)
|
|
text='N95口罩'
|
|
cv2.rectangle(frame, (int(item[1][0]), int(item[1][1])), (int(item[1][2]), int(item[1][3])), color, 5)
|
|
#添加文字
|
|
img = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
|
|
draw = ImageDraw.Draw(img)
|
|
font = ImageFont.truetype('simhei.ttf', 30, encoding="utf-8")
|
|
draw.text((int(item[1][0]), int(item[1][1])-35), text, rbg, font=font)
|
|
|
|
frame=cv2.cvtColor(numpy.asarray(img), cv2.COLOR_RGB2BGR)
|
|
|
|
# time.sleep(0.5)
|
|
|
|
# Display the resulting frame
|
|
cv2.imshow('frame', frame)
|
|
if cv2.waitKey(1) & 0xFF == ord('q'):
|
|
break
|
|
|
|
# When everything done, release the capture
|
|
cap.release()
|
|
cv2.destroyAllWindows() |