Author: UR的出不克
This is a web application system for satellite image object detection based on YOLOv5 and Flask. The system can detect and identify specific targets in satellite images, including oil tanks, vessels, bridges, and aircraft.
- Web interface for image upload and detection
- API interface support
- Real-time display of detection results and confidence levels
- Multiple object detection support
- Detection categories:
- Oil tanks (oilcan)
- Vessels (vessel)
- Bridges (bridge)
- Aircraft (plane)
- Python 3.7+
- PyTorch 1.7.0+
- Flask
- OpenCV
- Other dependencies listed in requirements.txt
- Clone the repository
git clone [repository_url]
cd yolo-flask-master
- Download YOLOv5
git clone https://github.com/ultralytics/yolov5.git
cd yolov5
pip install -r requirements.txt
cd ..
- Install project dependencies
pip install -r requirements.txt
- Prepare model file
- Place the trained model file
best.pt
in theyolov5
directory
- Create main.py
Create
main.py
file in theyolov5
directory with the following content:
import torch
from models.experimental import attempt_load
from utils.general import non_max_suppression
from utils.datasets import letterbox
import cv2
import numpy as np
def detect(weights, source, device):
# Load model
model = attempt_load(weights, device=device)
# Read image
img0 = cv2.imread(source)
img = letterbox(img0, 640, stride=32)[0]
img = img.transpose((2, 0, 1))[::-1]
img = np.ascontiguousarray(img)
img = torch.from_numpy(img).to(device)
img = img.float()
img /= 255.0
if img.ndimension() == 3:
img = img.unsqueeze(0)
# Inference
pred = model(img, augment=False)[0]
pred = non_max_suppression(pred, 0.25, 0.45)
# Process results
det = pred[0]
if len(det):
det[:, :4] = scale_coords(img.shape[2:], det[:, :4], img0.shape).round()
return det
def scale_coords(img1_shape, coords, img0_shape):
gain = min(img1_shape[0] / img0_shape[0], img1_shape[1] / img0_shape[1])
pad = (img1_shape[1] - img0_shape[1] * gain) / 2, (img1_shape[0] - img0_shape[0] * gain) / 2
coords[:, [0, 2]] -= pad[0]
coords[:, [1, 3]] -= pad[1]
coords[:, :4] /= gain
coords[:, 0].clamp_(0, img0_shape[1])
coords[:, 1].clamp_(0, img0_shape[0])
coords[:, 2].clamp_(0, img0_shape[1])
coords[:, 3].clamp_(0, img0_shape[0])
return coords
- Run the application
python app.py
-
Web Interface:
- Visit http://localhost:5000
- Click the upload button to select an image
- Click the "Object Detection" button to perform detection
- View detection results and confidence levels
-
API Usage:
- API endpoint: http://localhost:5000/api
- Request method: POST
- Request format: JSON
- Parameters:
{ "img_base64": "image base64 encoded string" }
- Backend: Flask
- Deep Learning Framework: YOLOv5 + PyTorch
- Frontend: Bootstrap
- Image Processing: OpenCV
Author: UR的出不克
MIT License