Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
2
23-064
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
23-064
23-064
Commits
34147840
Commit
34147840
authored
Aug 29, 2023
by
Thivanka Saparamadu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bug fix
parent
c7589a0c
Changes
10
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
315 additions
and
240 deletions
+315
-240
Mistaken Identification System/Mistakes Identify.ipynb
Mistaken Identification System/Mistakes Identify.ipynb
+0
-240
Mistaken Identification System/Mistakes_Identify.py
Mistaken Identification System/Mistakes_Identify.py
+55
-0
Mistaken Identification System/functions.py
Mistaken Identification System/functions.py
+77
-0
Mistaken Identification System/main.py
Mistaken Identification System/main.py
+117
-0
Mistaken Identification System/mistaken identification system.txt
... Identification System/mistaken identification system.txt
+0
-0
Mistaken Identification System/model/labels old.txt
Mistaken Identification System/model/labels old.txt
+12
-0
Mistaken Identification System/model/labels.txt
Mistaken Identification System/model/labels.txt
+15
-0
Mistaken Identification System/model/model.h5
Mistaken Identification System/model/model.h5
+0
-0
Mistaken Identification System/model/modelo old.h5
Mistaken Identification System/model/modelo old.h5
+0
-0
Mistaken Identification System/model_identification.py
Mistaken Identification System/model_identification.py
+39
-0
No files found.
Mistaken Identification System/Mistakes Identify.ipynb
deleted
100644 → 0
View file @
c7589a0c
This diff is collapsed.
Click to expand it.
Mistaken Identification System/Mistakes_Identify.py
0 → 100644
View file @
34147840
from
yolov7_package
import
Yolov7Detector
import
cv2
as
cv
from
functions
import
reSizeFrame
,
croppedImage
,
enhance_image
from
model_identification
import
vehicleIdentify
def
identifyMistakes
(
file
,
fps
,
objAccuracy
,
dimension
,
imgAccuracy
,
representYear
,
movie_name
):
# custom variables
fps_count
=
fps
# how many frames per second eg 1
object_accuracy
=
objAccuracy
# object accurate eg 0.5,0.6,0.8
image_accuracy
=
imgAccuracy
# object accurate eg 0.5,0.6,0.8
object_dimension
=
[
dimension
,
dimension
]
# object diamention [100, 100],[50, 50]
year
=
representYear
# static variables
det
=
Yolov7Detector
(
traced
=
False
)
color
=
(
0
,
0
,
255
)
i
=
0
c
=
0
cap
=
cv
.
VideoCapture
(
'./Uploads/{}'
.
format
(
file
))
fps
=
cap
.
get
(
cv
.
CAP_PROP_FPS
)
frameCount
=
int
(
fps
/
fps_count
)
while
cap
.
isOpened
():
currentFrameNo
=
cap
.
get
(
1
)
ret
,
img
=
cap
.
read
()
if
not
ret
:
break
frame_time_ms
=
cap
.
get
(
cv
.
CAP_PROP_POS_MSEC
)
if
int
(
currentFrameNo
)
==
c
:
resized_image
=
reSizeFrame
(
img
,
640
,
640
)
classes
,
boxes
,
scores
=
det
.
detect
(
resized_image
)
print
(
classes
)
# cv.imshow("image", resized_image)
if
cv
.
waitKey
(
1
)
&
0xFF
==
ord
(
'q'
):
break
for
i
,
a
in
enumerate
(
classes
[
0
]):
print
(
a
)
if
a
==
2
:
print
(
"vehicle Detected"
)
if
scores
[
0
][
i
]
>
object_accuracy
:
cropped_image
=
croppedImage
(
resized_image
,
boxes
[
0
][
i
][
1
],
boxes
[
0
][
i
][
3
],
boxes
[
0
][
i
][
0
],
boxes
[
0
][
i
][
2
])
if
cropped_image
.
shape
[
1
]
>
object_dimension
[
0
]
and
cropped_image
.
shape
[
0
]
>
object_dimension
[
1
]:
# enhanced_image = enhance_image(cropped_image)
vehicleIdentify
(
cropped_image
,
year
,
image_accuracy
,
frame_time_ms
,
resized_image
,
boxes
[
0
][
i
][
1
],
boxes
[
0
][
i
][
3
],
boxes
[
0
][
i
][
0
],
boxes
[
0
][
i
][
2
],
movie_name
,
i
)
i
+=
1
c
=
c
+
frameCount
Mistaken Identification System/functions.py
0 → 100644
View file @
34147840
import
cv2
as
cv
import
os
import
zipfile
import
shutil
ALLOWED_EXTENSIONS
=
{
'mp4'
,
'avi'
,
'mkv'
}
def
reSizeFrame
(
frame
,
width
:
int
,
height
:
int
):
dimentions
=
(
width
,
height
)
return
cv
.
resize
(
frame
,
dimentions
,
interpolation
=
cv
.
INTER_AREA
)
def
croppedImage
(
frame
,
index1
,
index3
,
index0
,
index2
):
return
frame
[
int
(
index1
):
int
(
index3
),
int
(
index0
):
int
(
index2
)]
# 1:3 ,0:2
def
allowed_file
(
filename
):
return
'.'
in
filename
and
\
filename
.
rsplit
(
'.'
,
1
)[
1
]
.
lower
()
in
ALLOWED_EXTENSIONS
def
enhance_image
(
image
,
h
=
10
,
hForColor
=
10
,
templateWindowSize
=
7
,
searchWindowSize
=
21
):
# Apply Non-local Means Denoising
return
cv
.
fastNlMeansDenoisingColored
(
image
,
None
,
h
,
hForColor
,
templateWindowSize
,
searchWindowSize
)
def
draw_rectangle
(
image
,
index1
,
index3
,
index0
,
index2
):
x1
,
y1
=
int
(
index0
),
int
(
index1
)
# Top-left corner
x2
,
y2
=
int
(
index2
),
int
(
index3
)
# Bottom-right corner
color
=
(
0
,
0
,
255
)
# red color
cv
.
rectangle
(
image
,
(
x1
,
y1
),
(
x2
,
y2
),
color
,
thickness
=
2
)
return
image
def
write_image
(
folder_name
,
image
,
time
,
image_no
,
obj_name
,
index1
,
index0
,):
text
=
str
(
obj_name
)
font
=
cv
.
FONT_HERSHEY_SIMPLEX
font_scale
=
0.5
font_color
=
(
0
,
0
,
255
)
# White color in BGR
thickness
=
2
position
=
(
int
(
index1
),
int
(
index0
))
# (x, y) coordinates of the top-left corner of the text
image_with_text
=
cv
.
putText
(
image
,
text
,
position
,
font
,
font_scale
,
font_color
,
thickness
)
seconds
,
milliseconds
=
divmod
(
int
(
time
),
1000
)
minutes
,
seconds
=
divmod
(
seconds
,
60
)
hours
,
minutes
=
divmod
(
minutes
,
60
)
file_name
=
str
(
str
(
hours
)
+
"-"
+
str
(
minutes
)
+
"-"
+
str
(
seconds
)
+
"_"
+
str
(
image_no
))
if
not
os
.
path
.
exists
(
"./Output/"
+
folder_name
):
os
.
makedirs
(
"./Output/{}/"
.
format
(
folder_name
))
cv
.
imwrite
(
'./Output/{}/{}.jpg'
.
format
(
folder_name
,
file_name
),
image_with_text
)
else
:
cv
.
imwrite
(
'./Output/{}/{}.jpg'
.
format
(
folder_name
,
file_name
),
image_with_text
)
def
zip_folder
(
folder_name
):
if
not
os
.
path
.
exists
(
"./Output/"
+
folder_name
):
return
# Name of the output ZIP archive
output_zip_filename
=
str
(
'./Output/{}.zip'
.
format
(
folder_name
))
# Name of the file you want to include in the ZIP archive
folder_to_zip
=
'./Output/{}'
.
format
(
folder_name
)
# Create a new ZIP archive and add the file to it
with
zipfile
.
ZipFile
(
output_zip_filename
,
'w'
,
zipfile
.
ZIP_DEFLATED
)
as
zipf
:
for
root
,
dirs
,
files
in
os
.
walk
(
folder_to_zip
):
for
file
in
files
:
file_path
=
os
.
path
.
join
(
root
,
file
)
arcname
=
os
.
path
.
relpath
(
file_path
,
folder_to_zip
)
zipf
.
write
(
file_path
,
arcname
)
# Reomove folder
shutil
.
rmtree
(
folder_to_zip
)
Mistaken Identification System/main.py
0 → 100644
View file @
34147840
from
flask
import
Flask
,
request
,
jsonify
,
send_file
from
pymongo
import
MongoClient
import
json
from
Mistakes_Identify
import
identifyMistakes
import
os
from
werkzeug.utils
import
secure_filename
from
functions
import
allowed_file
,
enhance_image
,
zip_folder
app
=
Flask
(
__name__
)
UPLOAD_FOLDER
=
'Uploads'
app
.
config
[
'UPLOAD_FOLDER'
]
=
UPLOAD_FOLDER
app
.
config
[
'MONGO_URI'
]
=
'mongodb+srv://thiva:KL6f2SewDJerXNPg@first.tqnzb.mongodb.net/Research?retryWrites=true&w=majority'
mongo
=
MongoClient
(
app
.
config
[
'MONGO_URI'
])
db
=
mongo
.
researchDB
@
app
.
route
(
'/clear/video'
,
methods
=
[
'GET'
])
def
clear_image
():
enhance_image
(
"./Images/images.jpeg"
,
"./Output"
)
return
jsonify
({
"message"
:
"Video cleared successfully."
}),
201
@
app
.
route
(
'/check/video'
,
methods
=
[
'POST'
])
def
analise_video
():
data
=
request
.
json
required_keys
=
[
'user_id'
,
'movie_name'
,
'file_name'
,
'fps_rate'
,
'obj_accuracy'
,
'obj_dimension'
,
'img_accuracy'
,
'year'
]
missing_keys
=
[
key
for
key
in
required_keys
if
key
not
in
data
]
if
missing_keys
:
missing_keys_str
=
', '
.
join
(
missing_keys
)
return
jsonify
({
"message"
:
f
"Required keys {missing_keys_str} are missing in the data."
}),
400
if
data
.
get
(
'user_id'
)
is
None
:
return
jsonify
({
"message"
:
"Please provide a user ID."
}),
400
if
data
.
get
(
'movie_name'
)
is
None
:
return
jsonify
({
"message"
:
"Please provide a movie name."
}),
400
if
data
.
get
(
'file_name'
)
is
None
:
return
jsonify
({
"message"
:
"Please provide a file name."
}),
400
if
data
.
get
(
'fps_rate'
)
is
None
:
return
jsonify
({
"message"
:
"Please provide a fps rate."
}),
400
if
data
.
get
(
'obj_accuracy'
)
is
None
:
return
jsonify
({
"message"
:
"Please provide a object accuracy."
}),
400
if
data
.
get
(
'obj_dimension'
)
is
None
:
return
jsonify
({
"message"
:
"Please provide a object dimension."
}),
400
if
data
.
get
(
'img_accuracy'
)
is
None
:
return
jsonify
({
"message"
:
"Please provide a image accuracy."
}),
400
if
data
.
get
(
'year'
)
is
None
:
return
jsonify
({
"message"
:
"Please provide a movie represent year."
}),
400
movie_name
=
str
(
data
.
get
(
'movie_name'
))
file_name
=
str
(
data
.
get
(
'file_name'
))
fps
=
int
(
data
.
get
(
'fps_rate'
))
objAccuracy
=
float
(
data
.
get
(
'obj_accuracy'
))
dimension
=
int
(
data
.
get
(
'obj_dimension'
))
imgAccuracy
=
float
(
data
.
get
(
'img_accuracy'
))
representYear
=
int
(
data
.
get
(
'year'
))
video_path
=
'./Uploads/{}'
.
format
(
file_name
)
if
not
os
.
path
.
exists
(
video_path
):
return
jsonify
({
"message"
:
"Movie file could not found"
}),
404
identifyMistakes
(
file_name
,
fps
,
objAccuracy
,
dimension
,
imgAccuracy
,
representYear
,
movie_name
)
zip_folder
(
movie_name
)
collection
=
db
.
researchCollection
inserted_data
=
collection
.
insert_one
(
data
)
if
inserted_data
:
return
jsonify
({
"message"
:
"Video analyzed successfully."
}),
200
else
:
return
jsonify
({
"message"
:
"Failed to save data"
}),
500
@
app
.
route
(
'/upload'
,
methods
=
[
'POST'
])
def
upload_file
():
if
'video'
not
in
request
.
files
:
return
jsonify
({
"message"
:
"Video could not found."
}),
400
file
=
request
.
files
[
'video'
]
if
file
.
filename
==
''
:
return
jsonify
({
"message"
:
"Please select a video."
}),
400
if
file
and
allowed_file
(
file
.
filename
):
filename
=
secure_filename
(
file
.
filename
)
filepath
=
os
.
path
.
join
(
app
.
config
[
'UPLOAD_FOLDER'
],
filename
)
file
.
save
(
filepath
)
return
jsonify
({
"message"
:
"Video uploaded successfully. Please wait for the results."
}),
201
else
:
return
jsonify
({
"message"
:
"Please upload a valid file format.(mp4, avi, mkv)"
}),
400
@
app
.
route
(
'/download/<filename>'
,
methods
=
[
'GET'
])
def
download_video
(
filename
):
folder_path
=
"./Output"
zip_filename
=
"{}.zip"
.
format
(
filename
)
zip_filepath
=
os
.
path
.
join
(
folder_path
,
zip_filename
)
os
.
system
(
f
"zip -r {zip_filepath} {folder_path}"
)
return
send_file
(
zip_filepath
,
as_attachment
=
True
,
download_name
=
'my_folder.zip'
)
@
app
.
route
(
'/movie/details/<userID>'
,
methods
=
[
'GET'
])
def
get_data
(
userID
):
collection
=
db
.
researchCollection
if
userID
:
data
=
list
(
collection
.
find
({
"user_id"
:
userID
}))
for
item
in
data
:
item
[
'_id'
]
=
str
(
item
[
'_id'
])
if
data
:
return
json
.
dumps
(
data
),
200
else
:
return
jsonify
({
"message"
:
"No data found"
}),
404
else
:
return
jsonify
({
"message"
:
"Missing user_id parameter"
}),
400
if
__name__
==
'__main__'
:
app
.
run
()
Mistaken Identification System/mistaken identification system.txt
deleted
100644 → 0
View file @
c7589a0c
Mistaken Identification System/model/labels old.txt
0 → 100644
View file @
34147840
0 2010-Alto
1 2015-Alto
2 2020-Alto
3 2020-Civic
4 2020-Corolla
5 2020-Hiace
6 2020-Hilux
7 2020-Prius
8 2020-Swift
9 2020-VagonR
10 2020-Vezel
11 2020-Vitz
Mistaken Identification System/model/labels.txt
0 → 100644
View file @
34147840
0 2020-Honda-Civic-Back
1 2020-Honda-Civic-Front
2 2020-Toyota-Aqua-Back
3 2020-Toyota-Aqua-Front
4 2020-Toyota-Prius-Back
5 2020-Toyota-Prius-Front
6 2020-Vitz-Back
7 2020-Vitz-Front
8 2007-Dimo-Batta
9 2012-Toyota-Prius-Front
10 2012-Toyota-Prius-Back
11 2009-Toyota-Prius-Back
12 2009-Toyota-Prius-Front
13 2010-Tata-Nano-Front
14 2010-Tata-Nano-Back
Mistaken Identification System/model/model.h5
0 → 100644
View file @
34147840
File added
Mistaken Identification System/model/modelo old.h5
0 → 100644
View file @
34147840
File added
Mistaken Identification System/model_identification.py
0 → 100644
View file @
34147840
from
keras.models
import
load_model
import
tensorflow
as
tf
import
numpy
as
np
from
functions
import
reSizeFrame
,
write_image
,
draw_rectangle
import
cv2
as
cv
def
vehicleIdentify
(
object_image
,
year
,
confidence_score
,
time
,
image
,
index1
,
index3
,
index0
,
index2
,
movie_name
,
image_no
):
np
.
set_printoptions
(
suppress
=
True
)
model
=
load_model
(
"./model/model.h5"
,
compile
=
False
)
class_names
=
open
(
"./model/labels.txt"
,
"r"
)
.
readlines
()
new_image
=
reSizeFrame
(
object_image
,
224
,
224
)
# cv.imshow("image", object_image)
# cv.waitKey()
X
=
tf
.
keras
.
utils
.
img_to_array
(
new_image
)
X
=
np
.
expand_dims
(
X
,
axis
=
0
)
image_data
=
np
.
vstack
([
X
])
prediction
=
model
.
predict
(
image_data
)
index
=
np
.
argmax
(
prediction
)
class_name
=
class_names
[
index
]
score
=
prediction
[
0
][
index
]
if
score
>
confidence_score
:
if
year
<
1920
:
print
(
"Unrelated Object Detected"
)
image_with_rectangle
=
draw_rectangle
(
image
,
index1
,
index3
,
index0
,
index2
)
write_image
(
movie_name
,
image_with_rectangle
,
time
,
image_no
,
"Unrelated"
,
index1
,
index0
)
# cv.imshow("Found Object Image", image_with_rectangle)
# cv.waitKey()
elif
year
<
int
(
class_name
[
2
:]
.
split
(
"-"
)[
0
]):
print
(
class_name
[
2
:])
print
(
"Confidence Score:"
,
score
)
image_with_rectangle
=
draw_rectangle
(
image
,
index1
,
index3
,
index0
,
index2
)
write_image
(
movie_name
,
image_with_rectangle
,
time
,
image_no
,
class_name
[
2
:],
index1
,
index0
)
# cv.imshow("Found Object Image", image_with_rectangle)
# cv.waitKey()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment