-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathetch_a_sketch_server.py
More file actions
71 lines (50 loc) · 1.89 KB
/
etch_a_sketch_server.py
File metadata and controls
71 lines (50 loc) · 1.89 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
from flask import Flask, request
from PIL import Image
import io
from pathlib import Path
from etch_a_sketch_cli import run_pipeline
import threading
import time
import shutil
UPLOAD_DIR = Path("uploads")
PROCESSING_DIR = Path("processing")
OUTPUT_DIR = Path("gcode_outputs")
app = Flask(__name__)
@app.route("/upload", methods=["POST"])
def upload_file():
print("Receiving image...")
file = request.files["image"]
image = Image.open(io.BytesIO(file.read()))
# Name image by timestamp MMDDHHMMSS
save_path = UPLOAD_DIR / f"{time.strftime('%m%d%H%M%S')}.png"
image.save(save_path)
return "Image processing started."
# Create a function that constantly checks UPLOAD DIR for new files
def process_images():
print("Processing thread started.")
while True:
for image_path in UPLOAD_DIR.iterdir():
print("Processing image:", image_path)
if image_path.suffix in [".jpg", ".jpeg", ".png"]:
processing_dir = PROCESSING_DIR / image_path.stem
processing_dir.mkdir()
run_pipeline(image_path, processing_dir, copy=True)
# Delete the image after processing
image_path.unlink()
# Copy gcode to output directory, named input file name.gcode
gcode_path = processing_dir / "output.gcode"
output_path = OUTPUT_DIR / f"{image_path.stem}.gcode"
shutil.copy(gcode_path, output_path)
time.sleep(1)
if __name__ == "__main__":
# Confirm that the output directory exists
if not UPLOAD_DIR.exists():
UPLOAD_DIR.mkdir()
if not PROCESSING_DIR.exists():
PROCESSING_DIR.mkdir()
if not OUTPUT_DIR.exists():
OUTPUT_DIR.mkdir()
# Start the processing thread
processing_thread = threading.Thread(target=process_images)
processing_thread.start()
app.run("0.0.0.0", debug=True)