-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_server.py
More file actions
73 lines (56 loc) · 1.99 KB
/
file_server.py
File metadata and controls
73 lines (56 loc) · 1.99 KB
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
import sys
sys.stdout.reconfigure(encoding='utf-8')
sys.stderr.reconfigure(encoding='utf-8')
"""
Local File Storage Server
=========================
Lightweight server that runs on your PC to receive and store images from the Pi.
Runs on port 5001 so it doesn't conflict with the main prediction server.
Usage:
python file_server.py
"""
import os
from datetime import datetime
from flask import Flask, request, jsonify
import config
app = Flask(__name__)
STORAGE_DIR = config.INCOMING_DIR
@app.route("/upload", methods=["POST"])
def upload():
"""Receive and store an image from the Pi."""
if "image" not in request.files:
return jsonify({"error": "No image file. Use 'image' form field."}), 400
file = request.files["image"]
if file.filename == "":
return jsonify({"error": "Empty filename"}), 400
os.makedirs(STORAGE_DIR, exist_ok=True)
save_path = os.path.join(STORAGE_DIR, file.filename)
file.save(save_path)
size_kb = os.path.getsize(save_path) / 1024
print(f"📥 Saved: {file.filename} ({size_kb:.1f} KB)")
return jsonify({
"status": "saved",
"filename": file.filename,
"size_kb": round(size_kb, 1),
"timestamp": datetime.now().isoformat(),
})
@app.route("/status", methods=["GET"])
def status():
"""Check server status and file count."""
os.makedirs(STORAGE_DIR, exist_ok=True)
files = [f for f in os.listdir(STORAGE_DIR)
if f.lower().endswith((".jpg", ".jpeg", ".png", ".bmp"))]
return jsonify({
"status": "running",
"storage_dir": STORAGE_DIR,
"files_stored": len(files),
})
if __name__ == "__main__":
os.makedirs(STORAGE_DIR, exist_ok=True)
print("=" * 50)
print("📁 Local File Storage Server")
print(f" Storing images in: {STORAGE_DIR}")
print(f" Upload: POST http://0.0.0.0:5001/upload")
print(f" Status: GET http://0.0.0.0:5001/status")
print("=" * 50)
app.run(host="0.0.0.0", port=5001, debug=False, threaded=True)