npc0 commited on
Commit
79f9dfb
1 Parent(s): 22122da

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -0
app.py ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ os.system('wget https://huggingface.co/spaces/An-619/FastSAM/resolve/main/weights/FastSAM.pt')
3
+
4
+ import yolov5
5
+
6
+ # load model
7
+ model = yolov5.load('keremberke/yolov5m-license-plate')
8
+
9
+ # set model parameters
10
+ model.conf = 0.5 # NMS confidence threshold
11
+ model.iou = 0.25 # NMS IoU threshold
12
+ model.agnostic = False # NMS class-agnostic
13
+ model.multi_label = False # NMS multiple labels per box
14
+ model.max_det = 1000 # maximum number of detections per image
15
+
16
+ # set image
17
+ def license_plate_detect(img):
18
+ # perform inference
19
+ results = model(img, size=640)
20
+
21
+ # inference with test time augmentation
22
+ results = model(img, augment=True)
23
+
24
+ # parse results
25
+ if len(results.pred):
26
+ predictions = results.pred[0]
27
+ boxes = predictions[:, :4] # x1, y1, x2, y2
28
+ scores = predictions[:, 4]
29
+ categories = predictions[:, 5]
30
+ return boxes
31
+
32
+ from PIL import Image
33
+ # image = Image.open(img)
34
+ import pytesseract
35
+
36
+ def read_license_number(img):
37
+ boxes = license_plate_detect(img)
38
+ if boxes:
39
+ return [pytesseract.image_to_string(
40
+ image.crop(bbox.tolist()))
41
+ for bbox in boxes]