MUHAMMEDHAFEEZ commited on
Commit
10bcc8b
1 Parent(s): bad27fe

Add application file

Browse files
Files changed (1) hide show
  1. app.py +61 -0
app.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import imghdr
3
+ import pytesseract
4
+
5
+ def extract_number_plate(image_path):
6
+ # Load the image
7
+ image = cv2.imread(image_path)
8
+
9
+ # Check if the image is valid
10
+ if image is None:
11
+ print("Invalid image file!")
12
+ return
13
+
14
+ # Convert the image to grayscale
15
+ gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
16
+
17
+ # Apply Gaussian blur to reduce noise
18
+ blurred = cv2.GaussianBlur(gray, (7, 7), 0)
19
+
20
+ # Perform edge detection using Canny algorithm
21
+ edges = cv2.Canny(blurred, 30, 150)
22
+
23
+ # Find contours in the edge-detected image
24
+ contours, _ = cv2.findContours(edges.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
25
+
26
+ # Filter contours based on area to select potential number plates
27
+ number_plate_contours = []
28
+ for contour in contours:
29
+ x, y, w, h = cv2.boundingRect(contour)
30
+ area = cv2.contourArea(contour)
31
+ if area > 1000 and w > h:
32
+ number_plate_contours.append(contour)
33
+
34
+ # Draw bounding rectangles around the number plates
35
+ for contour in number_plate_contours:
36
+ x, y, w, h = cv2.boundingRect(contour)
37
+ cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)
38
+
39
+ # Extract the region of interest (number plate)
40
+ plate = gray[y:y+h, x:x+w]
41
+
42
+ # Apply OCR to the number plate region
43
+ plate_text = pytesseract.image_to_string(plate, config='--psm 7')
44
+
45
+ # Print the extracted text
46
+ print("Number Plate Text:", plate_text)
47
+
48
+ # Display the image with bounding rectangles
49
+ cv2.imshow("Number Plates", image)
50
+ cv2.waitKey(0)
51
+ cv2.destroyAllWindows()
52
+
53
+ # Path to the input image
54
+ image_path = "cars/car2.jpg"
55
+
56
+ # Check if the file is an image
57
+ if imghdr.what(image_path) is not None:
58
+ # Extract the number plates and print the text
59
+ extract_number_plate(image_path)
60
+ else:
61
+ print("Invalid image file format!")