Skip to content

Commit 75e8f9b

Browse files
committed
project
0 parents  commit 75e8f9b

1,540 files changed

Lines changed: 132427 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 2,
6+
"id": "e35d918a",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": [
10+
"from paddleocr import PaddleOCR, draw_ocr # main OCR dependencies\n",
11+
"from matplotlib import pyplot as plt # plot images\n",
12+
"import cv2 #opencv"
13+
]
14+
},
15+
{
16+
"cell_type": "code",
17+
"execution_count": 9,
18+
"id": "93004937",
19+
"metadata": {
20+
"scrolled": true
21+
},
22+
"outputs": [
23+
{
24+
"name": "stdout",
25+
"output_type": "stream",
26+
"text": [
27+
"[2022/05/23 23:43:08] ppocr DEBUG: Namespace(help='==SUPPRESS==', use_gpu=False, ir_optim=True, use_tensorrt=False, min_subgraph_size=15, precision='fp32', gpu_mem=500, image_dir=None, det_algorithm='DB', det_model_dir='C:\\\\Users\\\\jpedr/.paddleocr/whl\\\\det\\\\en\\\\en_ppocr_mobile_v2.0_det_infer', det_limit_side_len=960, det_limit_type='max', det_db_thresh=0.3, det_db_box_thresh=0.6, det_db_unclip_ratio=1.5, max_batch_size=10, use_dilation=False, det_db_score_mode='fast', det_east_score_thresh=0.8, det_east_cover_thresh=0.1, det_east_nms_thresh=0.2, det_sast_score_thresh=0.5, det_sast_nms_thresh=0.2, det_sast_polygon=False, det_pse_thresh=0, det_pse_box_thresh=0.85, det_pse_min_area=16, det_pse_box_type='quad', det_pse_scale=1, scales=[8, 16, 32], alpha=1.0, beta=1.0, fourier_degree=5, det_fce_box_type='poly', rec_algorithm='CRNN', rec_model_dir='C:\\\\Users\\\\jpedr/.paddleocr/whl\\\\rec\\\\en\\\\en_number_mobile_v2.0_rec_infer', rec_image_shape='3, 32, 320', rec_batch_num=6, max_text_length=25, rec_char_dict_path='C:\\\\Users\\\\jpedr\\\\AppData\\\\Local\\\\Programs\\\\Python\\\\Python39\\\\lib\\\\site-packages\\\\paddleocr\\\\ppocr\\\\utils\\\\en_dict.txt', use_space_char=True, vis_font_path='./doc/fonts/simfang.ttf', drop_score=0.5, e2e_algorithm='PGNet', e2e_model_dir=None, e2e_limit_side_len=768, e2e_limit_type='max', e2e_pgnet_score_thresh=0.5, e2e_char_dict_path='./ppocr/utils/ic15_dict.txt', e2e_pgnet_valid_set='totaltext', e2e_pgnet_mode='fast', use_angle_cls=False, cls_model_dir='C:\\\\Users\\\\jpedr/.paddleocr/whl\\\\cls\\\\ch_ppocr_mobile_v2.0_cls_infer', cls_image_shape='3, 48, 192', label_list=['0', '180'], cls_batch_num=6, cls_thresh=0.9, enable_mkldnn=False, cpu_threads=10, use_pdserving=False, warmup=False, draw_img_save_dir='./inference_results', save_crop_res=False, crop_res_save_dir='./output', use_mp=False, total_process_num=1, process_id=0, benchmark=False, save_log_path='./log_output/', show_log=True, use_onnx=False, output='./output', table_max_len=488, table_model_dir=None, table_char_type='en', table_char_dict_path=None, layout_path_model='lp://PubLayNet/ppyolov2_r50vd_dcn_365e_publaynet/config', layout_label_map=None, mode='structure', lang='en', det=True, rec=True, type='ocr', ocr_version='PP-OCRv2', structure_version='STRUCTURE')\n"
28+
]
29+
}
30+
],
31+
"source": [
32+
"ocr_model = PaddleOCR(lang='en')\n"
33+
]
34+
},
35+
{
36+
"cell_type": "code",
37+
"execution_count": 10,
38+
"id": "16d43ff3",
39+
"metadata": {},
40+
"outputs": [
41+
{
42+
"name": "stdout",
43+
"output_type": "stream",
44+
"text": [
45+
"[2022/05/23 23:43:09] ppocr WARNING: Since the angle classifier is not initialized, the angle classifier will not be uesd during the forward process\n",
46+
"[2022/05/23 23:43:09] ppocr DEBUG: dt_boxes num : 1, elapse : 0.03198885917663574\n",
47+
"[2022/05/23 23:43:09] ppocr DEBUG: rec_res num : 1, elapse : 0.05398249626159668\n"
48+
]
49+
}
50+
],
51+
"source": [
52+
"result = ocr_model.ocr('cropped.jpg')"
53+
]
54+
},
55+
{
56+
"cell_type": "code",
57+
"execution_count": 13,
58+
"id": "8e2ce288",
59+
"metadata": {},
60+
"outputs": [
61+
{
62+
"data": {
63+
"text/plain": [
64+
"[[[[12.0, 11.0], [127.0, 26.0], [122.0, 52.0], [6.0, 37.0]],\n",
65+
" ('IZC840', 0.84395915)]]"
66+
]
67+
},
68+
"execution_count": 13,
69+
"metadata": {},
70+
"output_type": "execute_result"
71+
}
72+
],
73+
"source": [
74+
"result"
75+
]
76+
},
77+
{
78+
"cell_type": "code",
79+
"execution_count": 14,
80+
"id": "31e1ded4",
81+
"metadata": {},
82+
"outputs": [
83+
{
84+
"name": "stdout",
85+
"output_type": "stream",
86+
"text": [
87+
"IZC840\n"
88+
]
89+
}
90+
],
91+
"source": [
92+
"for res in result:\n",
93+
" print(res[1][0])"
94+
]
95+
},
96+
{
97+
"cell_type": "code",
98+
"execution_count": null,
99+
"id": "c0b2d187",
100+
"metadata": {},
101+
"outputs": [],
102+
"source": []
103+
}
104+
],
105+
"metadata": {
106+
"kernelspec": {
107+
"display_name": "Python 3 (ipykernel)",
108+
"language": "python",
109+
"name": "python3"
110+
},
111+
"language_info": {
112+
"codemirror_mode": {
113+
"name": "ipython",
114+
"version": 3
115+
},
116+
"file_extension": ".py",
117+
"mimetype": "text/x-python",
118+
"name": "python",
119+
"nbconvert_exporter": "python",
120+
"pygments_lexer": "ipython3",
121+
"version": "3.9.11"
122+
}
123+
},
124+
"nbformat": 4,
125+
"nbformat_minor": 5
126+
}

alpr_app/.ipynb_checkpoints/alpr-checkpoint.ipynb

Lines changed: 4407 additions & 0 deletions
Large diffs are not rendered by default.

alpr_app/.ipynb_checkpoints/alpr2-checkpoint.ipynb

Lines changed: 4449 additions & 0 deletions
Large diffs are not rendered by default.

alpr_app/Cropped.py

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
2+
from IPython.display import Image
3+
from matplotlib import pyplot as plt
4+
from PIL import Image, ImageFont, ImageDraw
5+
import cv2
6+
import numpy as np
7+
import pandas as pd
8+
import requests
9+
import time
10+
11+
#input_path = r'test_dataset/okk7448.jpg'
12+
13+
#Image(input_path)
14+
cap = cv2.VideoCapture(0)
15+
confThreshold = 0.5
16+
nmsThreshold = 0.4
17+
inpWidth = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
18+
inpHeight = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
19+
20+
classesFile = "C:/Users/jpedr/TCC/ALPR_final/alpr_app/yolo_utils/classes.names";
21+
classes = None
22+
with open(classesFile, 'rt') as f:
23+
classes = f.read().rstrip('\n').split('\n')
24+
25+
modelConfiguration = r"C:/Users/jpedr/TCC/ALPR_final/alpr_app/yolo_utils/darknet-yolov3.cfg";
26+
modelWeights = r"C:/Users/jpedr/TCC/ALPR_final/alpr_app/yolo_utils/lapi.weights";
27+
28+
net = cv2.dnn.readNetFromDarknet(modelConfiguration, modelWeights)
29+
net.setPreferableBackend(cv2.dnn.DNN_BACKEND_OPENCV)
30+
net.setPreferableTarget(cv2.dnn.DNN_TARGET_CPU)
31+
32+
33+
def getOutputsNames(net):
34+
# Get the names of all the layers in the network
35+
layersNames = net.getLayerNames()
36+
# Get the names of the output layers, i.e. the layers with unconnected outputs
37+
return [layersNames[i[0] - 1] for i in net.getUnconnectedOutLayers()]
38+
39+
def drawPred(classId, conf, left, top, right, bottom, frame):
40+
cv2.rectangle(frame, (left, top), (right, bottom), (0, 255, 0), 3)
41+
label = '%.2f' % conf
42+
if classes:
43+
assert(classId < len(classes))
44+
label = '%s:%s' % (classes[classId], label)
45+
labelSize, baseLine = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 0.5, 1)
46+
top = max(top, labelSize[1])
47+
cv2.rectangle(frame, (left, top - round(1.5*labelSize[1])), (left + round(1.5*labelSize[0]), top + baseLine), (0, 0, 255), cv2.FILLED)
48+
#cv.rectangle(frame, (left, top - round(1.5*labelSize[1])), (left + round(1.5*labelSize[0]), top + baseLine), (255, 255, 255), cv.FILLED)
49+
cv2.putText(frame, label, (left, top), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0,0,0), 2)
50+
51+
def postprocess(frame, outs):
52+
frameHeight = frame.shape[0]
53+
frameWidth = frame.shape[1]
54+
55+
56+
classIds = []
57+
confidences = []
58+
boxes = []
59+
for out in outs:
60+
61+
for detection in out:
62+
63+
scores = detection[5:]
64+
classId = np.argmax(scores)
65+
66+
confidence = scores[classId]
67+
'''if detection[4]>confThreshold:
68+
print(detection[4], " - ", scores[classId], " - th : ", confThreshold)
69+
print(detection)'''
70+
if confidence > confThreshold:
71+
center_x = int(detection[0] * frameWidth)
72+
center_y = int(detection[1] * frameHeight)
73+
width = int(detection[2] * frameWidth)
74+
height = int(detection[3] * frameHeight)
75+
left = int(center_x - width / 2)
76+
top = int(center_y - height / 2)
77+
classIds.append(classId)
78+
confidences.append(float(confidence))
79+
boxes.append([left, top, width, height])
80+
81+
82+
cropped=None
83+
indices = cv2.dnn.NMSBoxes(boxes, confidences, confThreshold, nmsThreshold)
84+
for i in indices:
85+
i = i[0]
86+
box = boxes[i]
87+
left = box[0]
88+
top = box[1]
89+
width = box[2]
90+
height = box[3]
91+
92+
93+
bottom = top + height
94+
right = left + width
95+
96+
97+
cropped = frame[top:bottom, left:right].copy()
98+
99+
drawPred(classIds[i], confidences[i], left, top, right, bottom, frame)
100+
if cropped is not None:
101+
return cropped
102+
103+
def put_text(image,position,text,size=16,color_text=(239, 255, 233)):
104+
im_pil = Image.fromarray(image)
105+
font = ImageFont.truetype("C:/Users/jpedr/TCC/ALPR_final/alpr_app/font.ttf", size=size)
106+
draw = ImageDraw.Draw(im_pil)
107+
draw.text(position, text, fill=color_text, font=font)
108+
image = np.asarray(im_pil)
109+
#image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
110+
return image
111+
112+
# RECONHECIMENTO DE PLACA
113+
class camera:
114+
115+
crawl= None
116+
captura = None
117+
print('iniciando camera')
118+
while cv2.waitKey(1) < 0:
119+
120+
hasFrame, frame = cap.read() #frame: an image object from cv2
121+
blob = cv2.dnn.blobFromImage(frame, 1/255, (inpWidth, inpHeight), [0,0,0], 1, crop=False)
122+
net.setInput(blob)
123+
outs = net.forward(getOutputsNames(net))
124+
cropped = postprocess(frame, outs)
125+
cv2.rectangle(frame,(0,0),(inpWidth,70),(32, 32, 32),-1)
126+
if cropped is not None:
127+
cv2.imwrite('C:/Users/jpedr/TCC/ALPR_final/alpr_app/cropped.jpg',cropped)
128+
url = 'http://127.0.0.1:5000/ocr'
129+
x = requests.post(url)
130+
placa=x.json()
131+
print(placa)
132+
frame = put_text(frame, (inpWidth -100,inpHeight -30),placa)
133+
if len(placa) == 7 and placa != crawl:
134+
try:
135+
all_infos = []
136+
url = 'http://127.0.0.1:5000/crawler'
137+
myobj = {'placa': placa}
138+
x = requests.post(url, json = myobj)
139+
info = x.json()
140+
#print(info)A
141+
if info:
142+
all_infos.append(info)
143+
plate_resource = info[-1]
144+
venal = info[0].replace(".",",")
145+
if venal == "Aliquota:": venal = "Não Estimado"
146+
else: venal = "R$ " + info[0].replace(".",",")
147+
marca = info[1] + ' '+info[2]+'/'+''+info[5]
148+
cidade = info[10]+'/'+info[9]
149+
headers = {"User-Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36"}
150+
req = requests.get(plate_resource,headers=headers).content
151+
plate = 'C:/Users/jpedr/TCC/ALPR_final/alpr_app/plate.webp'
152+
with open(plate, 'wb+') as f: f.write(req)
153+
frame = put_text(frame, (175,5),'Modelo: '+marca)
154+
frame = put_text(frame, (175,35),'Valor: '+venal)
155+
frame = put_text(frame, (375,35),'Local: '+cidade)
156+
try:
157+
plate = cv2.imread(plate)
158+
plate = cv2.resize(plate,(150,50))
159+
frame[10:10+50, 5:5+150] = plate
160+
except:
161+
pass
162+
crawl = placa
163+
captura=[marca,venal,cidade]
164+
except Exception as e:
165+
print(str(e))
166+
167+
#roi[np.where(mask)] = 0
168+
if captura != None:
169+
plate = 'C:/Users/jpedr/TCC/ALPR_final/alpr_app/plate.webp'
170+
frame = put_text(frame, (175,5),'Modelo: '+captura[0])
171+
frame = put_text(frame, (175,35),'Valor: '+captura[1])
172+
frame = put_text(frame, (375,35),'Local: '+captura[2])
173+
plate = cv2.imread(plate)
174+
plate = cv2.resize(plate,(150,50))
175+
frame[10:10+50, 5:5+150] = plate
176+
frames = frame
177+
#return frames
178+
#cv2.imshow("CAPTURA ALPR",frame)
179+
#time.sleep(1)
180+
181+
182+
#cam()
183+
184+
185+

0 commit comments

Comments
 (0)