Commit ec98c398 authored by Yashmika Anusara's avatar Yashmika Anusara

Merge branch 'master' of http://gitlab.sliit.lk/23-064/23-064

parents 9566c753 018db342
# Default ignored files
/shelf/
/workspace.xml
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="jdk" jdkName="Python 3.10" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="PyInterpreterInspection" enabled="true" level="INFORMATION" enabled_by_default="true" />
<inspection_tool class="PyPackageRequirementsInspection" enabled="true" level="WARNING" enabled_by_default="true">
<option name="ignoredPackages">
<value>
<list size="12">
<item index="0" class="java.lang.String" itemvalue="scikit-image" />
<item index="1" class="java.lang.String" itemvalue="scipy" />
<item index="2" class="java.lang.String" itemvalue="filterpy" />
<item index="3" class="java.lang.String" itemvalue="PyYAML" />
<item index="4" class="java.lang.String" itemvalue="torch" />
<item index="5" class="java.lang.String" itemvalue="requests" />
<item index="6" class="java.lang.String" itemvalue="torchvision" />
<item index="7" class="java.lang.String" itemvalue="ultralytics" />
<item index="8" class="java.lang.String" itemvalue="tqdm" />
<item index="9" class="java.lang.String" itemvalue="hydra-core" />
<item index="10" class="java.lang.String" itemvalue="matplotlib" />
<item index="11" class="java.lang.String" itemvalue="lap" />
</list>
</value>
</option>
</inspection_tool>
<inspection_tool class="PyPep8NamingInspection" enabled="true" level="WEAK WARNING" enabled_by_default="true">
<option name="ignoredErrors">
<list>
<option value="N806" />
<option value="N802" />
<option value="N803" />
</list>
</option>
</inspection_tool>
</profile>
</component>
\ No newline at end of file
<component name="InspectionProjectProfileManager">
<settings>
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.10" project-jdk-type="Python SDK" />
<component name="PyCharmProfessionalAdvertiser">
<option name="shown" value="true" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/Mistaken Identification System.iml" filepath="$PROJECT_DIR$/.idea/Mistaken Identification System.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
</component>
</project>
\ No newline at end of file
This diff is collapsed.
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
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)
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()
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
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
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()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment