-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
118 lines (100 loc) · 3.89 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
from flask import Flask, render_template, request, jsonify
from flask_cors import CORS
import numpy as np
import cv2
from ultralytics import YOLO
import cloudinary.uploader
from collections import Counter
from pymongo import MongoClient
import math
from datetime import datetime, timedelta
app = Flask(__name__)
app.json.ensure_ascii = False
CORS(app)
cloudinary.config(
cloud_name='dtldsdxbm',
api_key="583827442458891",
api_secret="EucQolFtyzct0gBQhe9lPy6RX_E"
)
uri = 'mongodb+srv://duonga1ne1:[email protected]/?retryWrites=true&w=majority&appName=Cluster0'
url = 'mongodb+srv://duonga1ne1:[email protected]/?retryWrites=true&w=majority&appName=Cluster0'
client = MongoClient(url)
db = client.jewelry_recognition
history_collection = db.history
@app.route('/')
def index():
return render_template('index.html')
@app.route('/history')
def history():
page = int(request.args.get('page', 1))
per_page = 6
start_date = request.args.get('start_date')
end_date = request.args.get('end_date')
print("Start date:", start_date)
print("End date:", end_date)
query = {}
sort_order = [('timestamp', -1)]
if start_date and end_date:
start = start_date
end = end_date
query = {"timestamp": {"$gte": datetime.strptime(start_date, '%Y-%m-%d'), "$lt": datetime.strptime(end_date, '%Y-%m-%d') + timedelta(days=1) }}
else:
start = None
end = None
print("Start:", start)
print("End:", end)
total_items = history_collection.count_documents(query)
total_pages = math.ceil(total_items / per_page)
list_items = history_collection.find(query)
total_items = list(list_items)
class_counts = {}
for item in total_items:
for obj in item['object_counts']:
class_name = obj['class']
count = obj['count']
if class_name in class_counts:
class_counts[class_name] += count
else:
class_counts[class_name] = count
paged_items_cursor = history_collection.find(query).sort(sort_order).skip((page - 1) * per_page).limit(per_page)
items = list(paged_items_cursor)
if start and end:
return render_template('history.html', history_items=items, page=page, total_pages=total_pages, start_date=start_date, end_date=end_date, class_counts=class_counts)
else:
return render_template('history.html', history_items=items, page=page, total_pages=total_pages, class_counts=class_counts)
@app.route('/detect', methods=['POST'])
def detect_objects():
try:
model = YOLO("best.pt")
file = request.files['image']
file_bytes = np.asarray(bytearray(file.read()), dtype=np.uint8)
img = cv2.imdecode(file_bytes, cv2.IMREAD_COLOR)
results = model.predict(img)
object_counts = []
for result in results:
names = result.names
counts = Counter(result.boxes.cls.tolist())
for class_id, count in counts.items():
object_counts.append({
"class": names[class_id],
"count": count
})
im_array = results[0].plot()
temp_image_path = "temp_image.jpg"
cv2.imwrite(temp_image_path, im_array)
cloudinary_response = cloudinary.uploader.upload(temp_image_path)
history_collection.insert_one({
"image_url": cloudinary_response['secure_url'],
"object_counts": object_counts,
"timestamp": datetime.now()
})
print(cloudinary_response['secure_url'])
return jsonify({
"object_counts": object_counts,
"image_url": cloudinary_response['secure_url']
}), 200
except Exception as e:
print(f"Error: {e}")
return jsonify({'error': 'An error occurred during processing.'}), 500
if __name__ == "__main__":
app.run(debug=True)