|
|
import base64 |
|
|
import cv2 |
|
|
import numpy as np |
|
|
from flask import Flask, request, jsonify, render_template |
|
|
import sys |
|
|
import io |
|
|
from ultralytics import YOLO |
|
|
import time |
|
|
from PIL import Image |
|
|
import torch |
|
|
|
|
|
|
|
|
|
|
|
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8') |
|
|
app = Flask(__name__) |
|
|
|
|
|
|
|
|
vegetables = [ |
|
|
"banana", "beans broad", "beans cluster", "beans haricot", "beetroot", |
|
|
"bitter guard", "bottle guard", "brinjal long", "brinjal[purple]", "cabbage", |
|
|
"capsicum green", "carrot", "cauliflower", "chilli green", "colocasia arvi", |
|
|
"corn", "cucumber", "drumstick", "garlic", "ginger", "ladies finger", |
|
|
"lemons", "Onion red", "potato", "sweet potato", "tomato", "Zuchini" |
|
|
] |
|
|
model = YOLO('fresh_model.pt') |
|
|
|
|
|
yolo_model = YOLO('another_model.pt') |
|
|
CONFIDENCE_THRESHOLD=0.6 |
|
|
|
|
|
@app.route('/') |
|
|
def index(): |
|
|
return render_template('index.html') |
|
|
|
|
|
@app.route('/predict', methods=['POST']) |
|
|
def predict(): |
|
|
try: |
|
|
|
|
|
data = request.json |
|
|
image_data = data['image'].split(',')[1] |
|
|
|
|
|
|
|
|
nparr = np.frombuffer(base64.b64decode(image_data), np.uint8) |
|
|
|
|
|
|
|
|
image = cv2.imdecode(nparr, cv2.IMREAD_COLOR) |
|
|
|
|
|
|
|
|
results = model(image) |
|
|
detections = results[0].boxes |
|
|
detected_vegetables = [] |
|
|
for box in detections: |
|
|
if box.conf > CONFIDENCE_THRESHOLD: |
|
|
class_index = int(box.cls[0].item()) |
|
|
|
|
|
|
|
|
vegetable_name = model.names[class_index] |
|
|
detected_vegetables.append(vegetable_name) |
|
|
detected_classes=detected_vegetables |
|
|
|
|
|
return jsonify({'predictions': detected_classes}) |
|
|
except Exception as e: |
|
|
return jsonify({'error': str(e)}), 500 |
|
|
|
|
|
|
|
|
|
|
|
@app.route('/detect', methods=['POST']) |
|
|
def detect(): |
|
|
try: |
|
|
|
|
|
data = request.json |
|
|
image_data = data['image'].split(',')[1] |
|
|
|
|
|
|
|
|
nparr = np.frombuffer(base64.b64decode(image_data), np.uint8) |
|
|
|
|
|
|
|
|
image = cv2.imdecode(nparr, cv2.IMREAD_COLOR) |
|
|
|
|
|
|
|
|
results = yolo_model(image) |
|
|
|
|
|
|
|
|
class_indices = results[0].boxes.cls.cpu().numpy().astype(int) |
|
|
unique_classes = np.unique(class_indices) |
|
|
|
|
|
|
|
|
|
|
|
detected_classes = [str(cls) for cls in unique_classes] |
|
|
|
|
|
return jsonify({'predictions': detected_classes}) |
|
|
except Exception as e: |
|
|
return jsonify({'error': str(e)}), 500 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
modeltext = YOLO('model_2.pt') |
|
|
|
|
|
@app.route('/ocr', methods=['POST']) |
|
|
def ocr(): |
|
|
try: |
|
|
|
|
|
data = request.json |
|
|
image_data = data['image'].split(',')[1] |
|
|
nparr = np.frombuffer(base64.b64decode(image_data), np.uint8) |
|
|
image = cv2.imdecode(nparr, cv2.IMREAD_COLOR) |
|
|
|
|
|
|
|
|
results = modeltext(image) |
|
|
detections = results[0].boxes |
|
|
high_conf_detections = [box for box in detections if box.conf > 0.55] |
|
|
|
|
|
|
|
|
text_present = len(high_conf_detections) > 0 |
|
|
|
|
|
|
|
|
return jsonify({'text_present': text_present}) |
|
|
except Exception as e: |
|
|
return jsonify({'error': str(e)}), 500 |
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__': |
|
|
app.run(host='0.0.0.0', port=7860, debug=True) |