importcsvimporttimeimportcv2importpyzbar.pyzbar as pyzbarfrom PIL importImage, ImageDraw, ImageFontimportosimportpygamefrom playsound importplaysoundimportthreading#定义自己的线程类
classmyThread (threading.Thread):def __init__(self, threadID, name, counter):
threading.Thread.__init__(self)
self.threadID=threadID
self.name=name
self.counter=counterdefrun(self):print ("开始线程:" +self.name)
face_detect_demo(url)print ("退出线程:" +self.name)#创建新线程
thread1 = myThread(1, "Thread-1", 1)#-----------------time----------
time1 = (time.strftime("%Y!%m@%d(%H*%M)%S`", time.localtime()))
time2= time1.replace(‘!‘, ‘年‘)
time3= time2.replace(‘@‘, ‘月‘)
time4= time3.replace(‘(‘, ‘日‘)
time5= time4.replace(‘*‘, ‘时‘)
time6= time5.replace(‘)‘, ‘分‘)
timenow= time6.replace(‘`‘, ‘秒‘)
timeTay= (time.strftime("%Y-%m-%d", time.localtime()))#-----------------time done----#标致跳出循环
barcodeData1 = ‘‘
#条形码摄像头为0
barCodeCapture =cv2.VideoCapture(0, cv2.CAP_DSHOW)
FaceCapture=cv2.VideoCapture(1, cv2.CAP_DSHOW)
Files= "./logo.txt" #新建一个日志,方便查阅(不需要可以删除)
takeAwayUrl=‘./Util/takeAway.mp3‘
#是否进行拍照
flag=Falsedefface_detect_demo(url):
FaceFlag, FaceFrame=FaceCapture.read()#计数用
num =0
gray=cv2.cvtColor(FaceFrame, cv2.COLOR_BGR2GRAY)#加载特征数据
face_detector =cv2.CascadeClassifier(‘Util/haarcascade_frontalface_default.xml‘)
faces= face_detector.detectMultiScale(gray,scaleFactor=1.88,minNeighbors=1)while len(faces) <=0:for face infaces:
x, y, w, h=face#print(x, y, w, h)
if w > 150 and h > 150:
cv2.rectangle(FaceFrame, (x, y), (x+ w, y + h), color=(0, 255, 0), thickness=2)
img_name="%s/%d.jpg" %(url,num)
cv2.imwrite(img_name,FaceFrame,[int(cv2.IMWRITE_PNG_COMPRESSION),9])
num+=1
if num>2:break
#保存图像,把灰度图片看成二维数组来检测人脸区域
#(这里是建立了data的文件夹,当然也可以设置为其他路径或者调用数据库)
while (1):
flag, frame=barCodeCapture.read()if notflag:breaktest=pyzbar.decode(frame)
gray=cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
barcodes=pyzbar.decode(gray)for barcode inbarcodes:
(x, y, w, h)=barcode.rect
cv2.rectangle(frame, (x, y), (x+ w, y + h), (255, 255, 0), 2)
barcodeData= barcode.data.decode("utf-8")
barcodeType=barcode.typeif barcodeData == ‘‘ or barcodeData !=barcodeData1:
barcodeData1=barcodeData
url=‘./‘+timeTay+‘/‘ + barcodeData+‘/‘
if notos.path.exists(url):
os.makedirs(url)
codeImgUrl= url+ ‘/条码图片.jpg‘
#======================可以不需要,这是在图片上进行标注,里面采用了字体文件==============================
img_PIL =Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
font= ImageFont.truetype(‘arialbd.ttf‘, 25) #前面是字体文件,后面是字号
fillColor = (0, 255, 0)
position= (x-25, y - 25)
strl=barcodeData
draw=ImageDraw.Draw(img_PIL)
draw.text(position, strl, font=font, fill=fillColor)
img_PIL.save(codeImgUrl,‘jpeg‘) #保存一张标记过的图片
#====================================================
#写入日志文件
with open(Files, ‘a+‘,encoding=‘utf-8‘) as w:
csv_write=csv.writer(w)
date= [‘类型:‘ + barcodeType + ‘条码:‘ + barcodeData + ‘时间:‘ +timenow]
csv_write.writerow(date)print("Recognize result>>> type: {0} content: {1}".format(barcodeType, barcodeData))#进行捕获脸部
#face_detect_demo(FaceFrame,url)
thread1.start()
thread1.join()print("退出线程")#os.system(takeAwayUrl)
playsound(takeAwayUrl)else:passcv2.imshow(‘codeBar‘, frame)#cv2.imshow(‘Face‘, FaceFrame)
k = cv2.waitKey(1)if k == 27:breakcv2.destroyAllWindows()
barCodeCapture.release()
FaceCapture.release()