訂閱
糾錯(cuò)
加入自媒體

如何使用Python+OpenCV+Keras實(shí)現(xiàn)無口罩車輛駕駛員懲罰生成

將Flask與兩個(gè)模型和MongoDB集成以實(shí)現(xiàn)端到端流程我們創(chuàng)建了一個(gè)flask main.py,該flask鏈接到各種HTML模板,以從用戶那里獲取前端汽車駕駛員圖像的輸入。然后,該圖像由CNN模型處理,以在后端進(jìn)行口罩檢測(cè),并且無論駕駛員是否戴口罩,結(jié)果都將顯示在HTML模板中。下面的代碼以圖像文件的形式從用戶那里獲取輸入,對(duì)圖像應(yīng)用各種預(yù)處理技術(shù),例如調(diào)整大小,灰度,重新排列圖像陣列,然后將圖像發(fā)送到已經(jīng)訓(xùn)練好的模型以確定輸出。@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.a(chǎn)ppend(resized)
       data=np.a(chǎn)rray(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模板作為上載圖像文件的一部分顯示給用戶。

下面是一個(gè)Html模板,當(dāng)POST方法在處理完圖像后發(fā)送結(jié)果時(shí)顯示,顯示駕駛員是否戴了口罩。

接下來,我們上傳車輛圖像,該圖像已被確定為駕駛員沒有戴口罩。車輛的圖像再次通過圖像預(yù)處理階段進(jìn)行處理,在該階段中,模型會(huì)嘗試從車牌中的車牌框中提取文本。@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.a(chǎn)rcLength(c, True)
           approx = cv2.a(chǎn)pproxPolyDP(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)
以下是車輛圖像上傳頁面,該頁面接收用戶的輸入并處理車輛圖像以獲得車牌號(hào)文字。

提取車牌編號(hào)的文本后,我們需要使用車號(hào)牌查找車牌持有人的詳細(xì)信息,接下來我們將連接到MongoDB創(chuàng)建的名為L(zhǎng)icense_Details的表。一旦獲得了車牌號(hào),名稱,地址等詳細(xì)信息,我們就可以生成罰款并將其顯示在HTML模板頁面上。

未來的工作與訓(xùn)練精度相比,口罩模型的測(cè)試精度要低得多。因此,未知數(shù)據(jù)集的錯(cuò)誤分類非常高。另外,我們需要努力提高基于OpenCV的車牌提取的準(zhǔn)確性,因?yàn)殄e(cuò)誤的關(guān)注區(qū)域可能會(huì)導(dǎo)致提取空的車牌文本。另外,可以進(jìn)一步改善前端,使其更具吸引力。參考

image.png


<上一頁  1  2  3  4  
聲明: 本文由入駐維科號(hào)的作者撰寫,觀點(diǎn)僅代表作者本人,不代表OFweek立場(chǎng)。如有侵權(quán)或其他問題,請(qǐng)聯(lián)系舉報(bào)。

發(fā)表評(píng)論

0條評(píng)論,0人參與

請(qǐng)輸入評(píng)論內(nèi)容...

請(qǐng)輸入評(píng)論/評(píng)論長(zhǎng)度6~500個(gè)字

您提交的評(píng)論過于頻繁,請(qǐng)輸入驗(yàn)證碼繼續(xù)

  • 看不清,點(diǎn)擊換一張  刷新

暫無評(píng)論

暫無評(píng)論

人工智能 獵頭職位 更多
掃碼關(guān)注公眾號(hào)
OFweek人工智能網(wǎng)
獲取更多精彩內(nèi)容
文章糾錯(cuò)
x
*文字標(biāo)題:
*糾錯(cuò)內(nèi)容:
聯(lián)系郵箱:
*驗(yàn) 證 碼:

粵公網(wǎng)安備 44030502002758號(hào)