订阅
纠错
加入自媒体

如何使用Python+OpenCV+Keras实现无口罩车辆驾驶员惩罚生成

2021-06-18 15:44
磐创AI
关注

将Flask与两个模型和MongoDB集成以实现端到端流程我们创建了一个flask main.py,该flask链接到各种HTML模板,以从用户那里获取前端汽车驾驶员图像的输入。然后,该图像由CNN模型处理,以在后端进行口罩检测,并且无论驾驶员是否戴口罩,结果都将显示在HTML模板中。下面的代码以图像文件的形式从用户那里获取输入,对图像应用各种预处理技术,例如调整大小,灰度,重新排列图像阵列,然后将图像发送到已经训练好的模型以确定输出。@app.route('/', methods=['POST'])
def upload_file():
   img_size=100
   data=[]
   uploaded_file = request.files['file']
   result=''
   if uploaded_file.filename != '':
       filename = uploaded_file.filename
       uploaded_file.save(os.path.join(app.config['UPLOAD_PATH'], filename))

      img_path = os.path.join(app.config['UPLOAD_PATH'], filename)
       print(img_path)
       img=cv2.imread(img_path)
       gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
       resized=cv2.resize(gray,(img_size,img_size))
       data.append(resized)
       data=np.array(data)/255.0
       data=np.reshape(data,(data.shape[0],img_size,img_size,1))
       
       new_model = load_model('saved_model/my_model')
       output = new_model.predict(data)
       
       if output[0][0]>=0.5:
           result = 'The person is Masked'
       else:
           result = 'The Person is Non Masked'
       print(result)
   return render_template('Show.html',result=result)
以下是HTML模板,该HTML模板作为上载图像文件的一部分显示给用户。

下面是一个Html模板,当POST方法在处理完图像后发送结果时显示,显示驾驶员是否戴了口罩。

接下来,我们上传车辆图像,该图像已被确定为驾驶员没有戴口罩。车辆的图像再次通过图像预处理阶段进行处理,在该阶段中,模型会尝试从车牌中的车牌框中提取文本。@app.route('/Vehicle', methods=['POST'])
def table2():
   uploaded_file = request.files['file']
   result=''
   if uploaded_file.filename != '':
       path='static/car'
       filename = uploaded_file.filename
       uploaded_file.save(os.path.join(path, filename))
       img_path = os.path.join(path, filename)
       print(img_path)
       img = cv2.imread(img_path,cv2.IMREAD_COLOR)
       img = cv2.resize(img, (600,400) )
       gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
       gray = cv2.bilateralFilter(gray, 13, 15, 15)
       edged = cv2.Canny(gray, 30, 200)
       contours = cv2.findContours(edged.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
       contours = imutils.grab_contours(contours)
       contours = sorted(contours, key = cv2.contourArea, reverse = True)[:10]
       screenCnt = None
   
       for c in contours:
           peri = cv2.arcLength(c, True)
           approx = cv2.approxPolyDP(c, 0.018 * peri, True)

           if len(approx) == 4:
               screenCnt = approx
               break
       if screenCnt is None:
           detected = 0
           print ("No contour detected")
       else:
           detected = 1  
       if detected == 1:
           cv2.drawContours(img, [screenCnt], -1, (0, 0, 255), 3)
       mask = np.zeros(gray.shape,np.uint8)
       new_image = cv2.drawContours(mask,[screenCnt],0,255,-1,)
       new_image = cv2.bitwise_and(img,img,mask=mask)
       (x, y) = np.where(mask == 255)
       (topx, topy) = (np.min(x), np.min(y))
       (bottomx, bottomy) = (np.max(x), np.max(y))
       Cropped = gray[topx:bottomx+1, topy:bottomy+1]
       text = pytesseract.image_to_string(Cropped, config='--psm 11')
       print("Detected license plate Number is:",text)    
      #text='GJW-1-15-A-1138'
       print('"{}"'.format(text))
       re.sub(r'[^-]',r'', text)
       text = text.replace("", " ")
       text = re.sub('[W_]+', '', text)
       print(text)
       print('"{}"'.format(text))
       query1 = {"Number Plate": text}
       print("0")
       for doc in collection.find(query1):
          doc1 = doc
       Name=doc1['Name']
       Address=doc1['Address']
       License=doc1['License Number']
   return render_template('Penalty.html',Name=Name,Address=Address,License=License)
以下是车辆图像上传页面,该页面接收用户的输入并处理车辆图像以获得车牌号文字。

提取车牌编号的文本后,我们需要使用车号牌查找车牌持有人的详细信息,接下来我们将连接到MongoDB创建的名为License_Details的表。一旦获得了车牌号,名称,地址等详细信息,我们就可以生成罚款并将其显示在HTML模板页面上。

未来的工作与训练精度相比,口罩模型的测试精度要低得多。因此,未知数据集的错误分类非常高。另外,我们需要努力提高基于OpenCV的车牌提取的准确性,因为错误的关注区域可能会导致提取空的车牌文本。另外,可以进一步改善前端,使其更具吸引力。参考

image.png


<上一页  1  2  3  4  
声明: 本文由入驻维科号的作者撰写,观点仅代表作者本人,不代表OFweek立场。如有侵权或其他问题,请联系举报。

发表评论

0条评论,0人参与

请输入评论内容...

请输入评论/评论长度6~500个字

您提交的评论过于频繁,请输入验证码继续

暂无评论

暂无评论

人工智能 猎头职位 更多
扫码关注公众号
OFweek人工智能网
获取更多精彩内容
文章纠错
x
*文字标题:
*纠错内容:
联系邮箱:
*验 证 码:

粤公网安备 44030502002758号