NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

from flask import Flask, request, jsonify, render_template
import cv2
import base64
import numpy as np
import os
import requests
from roboflow import Roboflow
import time

app = Flask(__name__)

# গ্লোবাল ভেরিয়েবল ডিটেকশন স্টেট ট্র্যাক করার জন্য
last_detection_time = 0
detection_cooldown = 2 # 2 সেকেন্ড কো-লডাউন পিরিয়ড

# Roboflow সেটআপ
rf = Roboflow(api_key="GiFvO4SzDxqs4Jv9fTiE")
project = rf.workspace().project("smart-dustbin-yhsey")
model = project.version(1).model

# অবজেক্ট কাউন্টার
object_counts = {
'plastic': 0,
'metal': 0,
'organic': 0,
'drywaste': 0

}

backend_url = 'http://127.0.0.1:8000/api/detect/'

@app.route('/')
def index():
return render_template('index.html')

@app.route('/predict', methods=['POST'])
def predict():
global last_detection_time

current_time = time.time()

# কো-লডাউন পিরিয়ড চেক করা
if current_time - last_detection_time < detection_cooldown:
return jsonify({
"status": "cooldown",
"message": f"Waiting {detection_cooldown} seconds between detections"
}), 200

data = request.get_json()
if 'image' not in data:
return jsonify({'error': 'No image provided'}), 400

image_data = data['image'].split(',')[1]
image_bytes = base64.b64decode(image_data)

np_arr = np.frombuffer(image_bytes, np.uint8)
img = cv2.imdecode(np_arr, cv2.IMREAD_COLOR)

temp_filename = "frame.jpg"
cv2.imwrite(temp_filename, img)

# কনফিডেন্স লেভেল 50 এ সেট করা
prediction = model.predict(temp_filename, confidence=50, overlap=30).json()
predictions = prediction.get("predictions", [])

global object_counts
object_counts = {k: 0 for k in object_counts}

for det in predictions:
class_name = det['class'].lower()
if class_name in object_counts:
object_counts[class_name] += 1

result = {
"predictions": predictions,
"total_objects": sum(object_counts.values()),
"object_counts": object_counts
}

# শুধুমাত্র প্রথম ডিটেক্টেড অবজেক্ট প্রসেস করা
if sum(object_counts.values()) > 0:
send_first_detected_object(object_counts)
last_detection_time = time.time() # শেষ ডিটেকশনের টাইম আপডেট করা

if os.path.exists(temp_filename):
os.remove(temp_filename)

return jsonify(result)

def send_first_detected_object(object_counts):
priority_order = ['plastic', 'metal', 'drywaste', 'organic']

detected_class = None
count = 0

for class_name in priority_order:
if object_counts.get(class_name, 0) > 0:
detected_class = class_name
count = object_counts[class_name]
break

if not detected_class:
print("No objects detected to send to backend")
return

data = {
'detected_class': detected_class,
'count': count
}

try:
response = requests.post(backend_url, json=data)
print(f"Sent data to backend: {response.status_code} {response.text}")
except Exception as e:
print(f"Error sending data to backend: {e}")

if __name__ == '__main__':
app.run(debug=True)

# app.run(host='"
and views.py"from rest_
     
 
what is notes.io
 

Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 14 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.