如何使用Python+OpenCV+Keras實現(xiàn)無口罩車輛駕駛員懲罰生成
將Flask與兩個模型和MongoDB集成以實現(xiàn)端到端流程我們創(chuàng)建了一個flask main.py,該flask鏈接到各種HTML模板,以從用戶那里獲取前端汽車駕駛員圖像的輸入。然后,該圖像由CNN模型處理,以在后端進(jìn)行口罩檢測,并且無論駕駛員是否戴口罩,結(jié)果都將顯示在HTML模板中。下面的代碼以圖像文件的形式從用戶那里獲取輸入,對圖像應(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模板作為上載圖像文件的一部分顯示給用戶。
下面是一個Html模板,當(dāng)POST方法在處理完圖像后發(fā)送結(jié)果時顯示,顯示駕駛員是否戴了口罩。
接下來,我們上傳車輛圖像,該圖像已被確定為駕駛員沒有戴口罩。車輛的圖像再次通過圖像預(yù)處理階段進(jìn)行處理,在該階段中,模型會嘗試從車牌中的車牌框中提取文本。@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)
以下是車輛圖像上傳頁面,該頁面接收用戶的輸入并處理車輛圖像以獲得車牌號文字。
提取車牌編號的文本后,我們需要使用車號牌查找車牌持有人的詳細(xì)信息,接下來我們將連接到MongoDB創(chuàng)建的名為License_Details的表。一旦獲得了車牌號,名稱,地址等詳細(xì)信息,我們就可以生成罰款并將其顯示在HTML模板頁面上。
未來的工作與訓(xùn)練精度相比,口罩模型的測試精度要低得多。因此,未知數(shù)據(jù)集的錯誤分類非常高。另外,我們需要努力提高基于OpenCV的車牌提取的準(zhǔn)確性,因為錯誤的關(guān)注區(qū)域可能會導(dǎo)致提取空的車牌文本。另外,可以進(jìn)一步改善前端,使其更具吸引力。參考

請輸入評論內(nèi)容...
請輸入評論/評論長度6~500個字
最新活動更多
推薦專題
- 1 UALink規(guī)范發(fā)布:挑戰(zhàn)英偉達(dá)AI統(tǒng)治的開始
- 2 北電數(shù)智主辦酒仙橋論壇,探索AI產(chǎn)業(yè)發(fā)展新路徑
- 3 降薪、加班、裁員三重暴擊,“AI四小龍”已折戟兩家
- 4 “AI寒武紀(jì)”爆發(fā)至今,五類新物種登上歷史舞臺
- 5 國產(chǎn)智駕迎戰(zhàn)特斯拉FSD,AI含量差幾何?
- 6 光計算迎來商業(yè)化突破,但落地仍需時間
- 7 東陽光:2024年扭虧、一季度凈利大增,液冷疊加具身智能打開成長空間
- 8 地平線自動駕駛方案解讀
- 9 封殺AI“照騙”,“淘寶們”終于不忍了?
- 10 優(yōu)必選:營收大增主靠小件,虧損繼續(xù)又逢關(guān)稅,能否乘機(jī)器人東風(fēng)翻身?