Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
234 changes: 234 additions & 0 deletions dev_New_project/LiveCarNumberPlateDetector.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,234 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"05NB1786\n",
"KA95NB1786\n",
"KA95N81786\n",
"KA05NB1786\n"
]
}
],
"source": [
"import cv2\n",
"import numpy as np\n",
"from imutils.object_detection import non_max_suppression\n",
"\n",
"## ----------- Load Pre-trained Models ------------\n",
"model = cv2.dnn.readNet('frozen_east_text_detection.pb')\n",
"model1 = cv2.dnn.readNet('crnn.onnx')\n",
"\n",
"def most_likely(scores, char_set):\n",
" text = \"\"\n",
" for i in range(scores.shape[0]):\n",
" c = np.argmax(scores[i][0])\n",
" text += char_set[c]\n",
" return text\n",
"\n",
"def map_rule(text):\n",
" char_list = []\n",
" for i in range(len(text)):\n",
" if i == 0:\n",
" if text[i] != '-':\n",
" char_list.append(text[i])\n",
" else:\n",
" if text[i] != '-' and (not (text[i] == text[i - 1])):\n",
" char_list.append(text[i])\n",
" return ''.join(char_list)\n",
"\n",
"def best_path(scores, char_set):\n",
" text = most_likely(scores, char_set)\n",
" final_text = map_rule(text)\n",
" return final_text\n",
"\n",
"alphabet_set = \"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ\"\n",
"blank = '-'\n",
"\n",
"char_set = blank + alphabet_set\n",
"\n",
"def mainfn():\n",
" #img = cv2.imread(\"kia-seltos-car-number-plate-designs.jpeg\")\n",
" img = cv2.imread(\"pic.jpg\")\n",
" # use multiple of 32 to set the new img shape\n",
" height, width, _ = img.shape\n",
" new_height = (height//32)*32\n",
" new_width = (width//32)*32\n",
"\n",
" # get the ratio change in width and height\n",
" h_ratio = height/new_height\n",
" w_ratio = width/new_width\n",
"\n",
" blob = cv2.dnn.blobFromImage(img,1.0, (new_width, new_height), (123.68, 116.78, 103.94), True, False)\n",
"\n",
" model.setInput(blob)\n",
" (geometry, scores) = model.forward(model.getUnconnectedOutLayersNames())\n",
"\n",
" rectangles = []\n",
" confidence_score = []\n",
" for i in range(geometry.shape[2]):\n",
" for j in range(0, geometry.shape[3]):\n",
"\n",
" if scores[0][0][i][j] < 0.1:\n",
" continue\n",
"\n",
" bottom_x = int(j*4 + geometry[0][1][i][j])\n",
" bottom_y = int(i*4 + geometry[0][2][i][j])\n",
"\n",
"\n",
" top_x = int(j*4 - geometry[0][3][i][j])\n",
" top_y = int(i*4 - geometry[0][0][i][j])\n",
"\n",
" rectangles.append((top_x, top_y, bottom_x, bottom_y))\n",
" confidence_score.append(float(scores[0][0][i][j]))\n",
"\n",
" # use Non-max suppression to get the required rectangles\n",
" fin_boxes = non_max_suppression(np.array(rectangles), probs=confidence_score, overlapThresh=0.5)\n",
"\n",
" img_copy = img.copy()\n",
" chara = {}\n",
" for (x1, y1, x2, y2) in fin_boxes:\n",
"\n",
" x1 = int(x1 * w_ratio)\n",
" y1 = int(y1 * h_ratio)\n",
" x2 = int(x2 * w_ratio)\n",
" y2 = int(y2 * h_ratio)\n",
" \n",
" segment = img[y1:y2, x1:x2, :]\n",
" #segment = img[y1:y2, x1:x2, :]\n",
" segment_gray = cv2.cvtColor(segment, cv2.COLOR_BGR2GRAY)\n",
" \n",
" blob = cv2.dnn.blobFromImage(segment_gray, scalefactor=1/127.5, size=(100,32), mean=127.5)\n",
" model1.setInput(blob)\n",
" scores = model1.forward()\n",
" text = best_path(scores, char_set)\n",
" chara[x1] = text\n",
" #print(text)\n",
" \n",
" cv2.rectangle(img_copy, (x1, y1), (x2, y2), (0, 255, 0), 2)\n",
" cv2.putText(img_copy, text.strip(), (x1,y1-2), cv2.FONT_HERSHEY_COMPLEX, 0.5, (0,0,255),2)\n",
"\n",
" final=[]\n",
" for j in sorted(chara):\n",
" final.append(chara[j])\n",
" #print(final)\n",
" s = \"\"\n",
" s = s.join(final)\n",
" return s\n",
"\n",
"## ---------- Performing OCR in live video ----------\n",
"\n",
"cap = cv2.VideoCapture(0)\n",
"count=0\n",
"while cap.isOpened():\n",
" \n",
" count=count+1\n",
" ret, img = cap.read()\n",
" if count == 10:\n",
" cv2.imwrite(\"pic.jpg\", img)\n",
" #print(\"pic clicked\")\n",
" output_val = mainfn()\n",
" print(output_val) \n",
" #break\n",
" count=0\n",
" # use multiple of 32 to set the new img shape\n",
" height, width, _ = img.shape\n",
" new_height = (height//32)*32\n",
" new_width = (width//32)*32\n",
"\n",
" # get the ratio change in width and height\n",
" h_ratio = height/new_height\n",
" w_ratio = width/new_width\n",
"\n",
" blob = cv2.dnn.blobFromImage(img,1.0, (new_width, new_height), (123.68, 116.78, 103.94), True, False)\n",
"\n",
" model.setInput(blob)\n",
" (geometry, scores) = model.forward(model.getUnconnectedOutLayersNames())\n",
"\n",
" rectangles = []\n",
" confidence_score = []\n",
" for i in range(geometry.shape[2]):\n",
" for j in range(0, geometry.shape[3]):\n",
"\n",
" if scores[0][0][i][j] < 0.1:\n",
" continue\n",
"\n",
" bottom_x = int(j*4 + geometry[0][1][i][j])\n",
" bottom_y = int(i*4 + geometry[0][2][i][j])\n",
"\n",
"\n",
" top_x = int(j*4 - geometry[0][3][i][j])\n",
" top_y = int(i*4 - geometry[0][0][i][j])\n",
"\n",
" rectangles.append((top_x, top_y, bottom_x, bottom_y))\n",
" confidence_score.append(float(scores[0][0][i][j]))\n",
"\n",
" # use Non-max suppression to get the required rectangles\n",
" fin_boxes = non_max_suppression(np.array(rectangles), probs=confidence_score, overlapThresh=0.5)\n",
"\n",
" img_copy = img.copy()\n",
" for (x1, y1, x2, y2) in fin_boxes:\n",
"\n",
" x1 = int(x1 * w_ratio)\n",
" y1 = int(y1 * h_ratio)\n",
" x2 = int(x2 * w_ratio)\n",
" y2 = int(y2 * h_ratio)\n",
" \n",
" #segment = img[y1:y2+4, x1:x2+2, :]\n",
" segment = img[y1:y2, x1:x2, :]\n",
" segment_gray = cv2.cvtColor(segment, cv2.COLOR_BGR2GRAY)\n",
" \n",
" blob = cv2.dnn.blobFromImage(segment_gray, scalefactor=1/127.5, size=(100,32), mean=127.5)\n",
" model1.setInput(blob)\n",
" scores = model1.forward()\n",
" text = best_path(scores, char_set)\n",
" #print(text)\n",
"\n",
" cv2.rectangle(img_copy, (x1, y1), (x2, y2), (0, 255, 0), 2)\n",
" cv2.putText(img_copy, text.strip(), (x1,y1-2), cv2.FONT_HERSHEY_COMPLEX, 0.5, (0,0,255),2)\n",
"\n",
" cv2.imshow(\"Text Detection\", img_copy)\n",
" if cv2.waitKey(1) == 113:\n",
" break\n",
"\n",
"cap.release()\n",
"cv2.destroyAllWindows() \n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.3"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
1 change: 1 addition & 0 deletions dev_New_project/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
All about project
27 changes: 27 additions & 0 deletions dev_New_project/cgi-bin/api/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import requests
import json

BASE = 'http://127.0.0.1:5000/'

data = [{'Registration': 'MH01AV8866',
'Owner': 'M//S PANDURONGA TIMBLO INDUSTRIES',
'Maker': 'JAGUAR LAND ROVER INDIA LIMITED/ JAGUAR(XF 5.0LV8',
'Vehicle': 'MOTOR CAR (L)',
'Fuel_Type': 'PETROL',
'Chassis': 'SAJAAC07P8BLRXXXXX',
'Engine_Number': '10052323430XXXXX',
'Registration_Date': '22-FEB-2011',
'Insurance_Valid_Upto': '17-FEB-2012'
}

]

for i in range(len(data)):
response = requests.put(BASE + "vehicle/" + data[i]['Registration'], data[i])
print(response.json())

input()

response = requests.get(BASE + "vehicle/"+ data[0]['Registration'])

print(json.dumps(response.json(), indent=1))
93 changes: 93 additions & 0 deletions dev_New_project/cgi-bin/api/rto-api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
'''
Install all these packages with pip
Flask==1.1.2
Flask-RESTful==0.3.8
Flask-SQLAlchemy==2.4.3
Jinja2==2.11.2
SQLAlchemy==1.3.18
'''

from flask import Flask
from flask_restful import Api, Resource, reqparse, abort, fields, marshal_with
from flask_sqlalchemy import SQLAlchemy


app = Flask(__name__)
#Restful
api = Api(app)
# Creating/loading sqlite data base. Can be treated as fake database fo RTO
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///rto.db'
db = SQLAlchemy(app)

# Adding the columns to the model.
class RtoModel(db.Model):
Registration = db.Column(db.String(64), primary_key=True)
Owner = db.Column(db.String(64), nullable=False)
Maker = db.Column(db.String(64), nullable=False)
Vehicle = db.Column(db.String(64), nullable=False)
Fuel_Type = db.Column(db.String(64), nullable=False)
Chassis = db.Column(db.String(64), nullable=False)
Engine_Number = db.Column(db.String(64), nullable=False)
Registration_Date = db.Column(db.String(64), nullable=False)
Insurance_Valid_Upto = db.Column(db.String(64), nullable=False)

def __repr__(self):
return f"RTO( Registration={Registration}, Owner={Owner}, Maker={Maker}, Vehicle={Vehicle}, Fuel_Type={Fuel_Type}, Chassis={Chassis}, Engine_Number={Engine_Number}, Registration_Date={Registration_Date}, Insurance_Valid_Upto={Insurance_Valid_Upto})"


# do the below thing only once for creating the table.
#db.create_all()

video_put_args = reqparse.RequestParser()
video_put_args.add_argument('Registration', type=str, help='Registration number is required', required=True)
video_put_args.add_argument('Owner', type=str, help="Owner's Name is required", required=True)
video_put_args.add_argument('Maker', type=str, help='Maker details are required', required=True)
video_put_args.add_argument('Vehicle', type=str, help='Vehicle type is required', required=True)
video_put_args.add_argument('Fuel_Type', type=str, help='Type of Fuel is required', required=True)
video_put_args.add_argument('Chassis', type=str, help='Chassis Number is required', required=True)
video_put_args.add_argument('Engine_Number', type=str, help='Engine Number is required', required=True)
video_put_args.add_argument('Registration_Date', type=str, help='Registration Date is required', required=True)
video_put_args.add_argument('Insurance_Valid_Upto', type=str, help='Insurance Due Date is required', required=True)


Resource_fields = {
'Registration' : fields.String,
'Owner': fields.String,
'Maker': fields.String,
'Vehicle': fields.String,
'Fuel_Type': fields.String,
'Chassis': fields.String,
'Engine_Number': fields.String,
'Registration_Date': fields.String,
'Insurance_Valid_Upto': fields.String
}


class RTO(Resource):
@marshal_with(Resource_fields)
def get(self, vehicle_number):
result = RtoModel.query.filter_by(Registration=vehicle_number).first()
if not result:
abort(404, message="Vahicle details Not found...")
return result

@marshal_with(Resource_fields)
def put(self, vehicle_number):
args = video_put_args.parse_args()
result = RtoModel.query.filter_by(Registration=vehicle_number).first()
if result:
abort(409, message="Vehicle already exists...")
vehicle = RtoModel(Registration=vehicle_number, Owner=args['Owner'], Maker=args['Maker'], Vehicle=args['Vehicle'], Fuel_Type=args['Fuel_Type'], Chassis=args['Chassis'], Engine_Number=args['Engine_Number'], Registration_Date=args['Registration_Date'], Insurance_Valid_Upto=args['Insurance_Valid_Upto'])
db.session.add(vehicle)
db.session.commit()
return vehicle , 201

def delete(self, video_id):
return "", 204

api.add_resource(RTO, "/vehicle/<string:vehicle_number>")



if __name__ == '__main__':
app.run(debug=True)
Binary file added dev_New_project/cgi-bin/api/rto.db
Binary file not shown.
19 changes: 19 additions & 0 deletions dev_New_project/cgi-bin/get-info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/python3

print("content-type: text/html")
print()

import cgi
import requests
import json

BASE = 'http://127.0.0.1:5000/'

field = cgi.FieldStorage()
vehicle_number = field.getvalue("vehicle_number")


response = requests.get(BASE + "vehicle/" + vehicle_number)
responseJson = json.dumps(response.json(), indent=1)

print(responseJson.replace("{", "").replace("}", ""))
Binary file added dev_New_project/crnn.onnx
Binary file not shown.
Binary file added dev_New_project/frozen_east_text_detection.pb
Binary file not shown.
Loading