399 lines
11 KiB
Go
399 lines
11 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"log"
|
|
"log/slog"
|
|
"net"
|
|
"net/http"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"regexp"
|
|
"sort"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"unified-dashboard/internal/common"
|
|
mymiddleware "unified-dashboard/internal/middleware"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
"github.com/go-chi/chi/v5/middleware"
|
|
"github.com/go-chi/cors"
|
|
)
|
|
|
|
var (
|
|
baseDir string
|
|
uploadFolder string
|
|
processedFolder string
|
|
resultsFolder string
|
|
coordinatesFile string
|
|
resultsJSONFile string
|
|
workerPort string
|
|
)
|
|
|
|
func initPaths() {
|
|
cwd, _ := os.Getwd()
|
|
baseDir = filepath.Join(cwd, "data", "facial-rotation-detector")
|
|
uploadFolder = filepath.Join(baseDir, "uploads")
|
|
processedFolder = filepath.Join(baseDir, "processed")
|
|
resultsFolder = filepath.Join(baseDir, "results")
|
|
coordinatesFile = filepath.Join(resultsFolder, "coordinates.txt")
|
|
resultsJSONFile = filepath.Join(resultsFolder, "results.json")
|
|
|
|
os.MkdirAll(uploadFolder, 0755)
|
|
os.MkdirAll(processedFolder, 0755)
|
|
os.MkdirAll(resultsFolder, 0755)
|
|
}
|
|
|
|
// getFreePort asks the kernel for a free open port
|
|
func getFreePort() (string, error) {
|
|
addr, err := net.ResolveTCPAddr("tcp", "localhost:0")
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
l, err := net.ListenTCP("tcp", addr)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
defer l.Close()
|
|
return strconv.Itoa(l.Addr().(*net.TCPAddr).Port), nil
|
|
}
|
|
|
|
func startPythonWorker() {
|
|
var err error
|
|
workerPort, err = getFreePort()
|
|
if err != nil {
|
|
log.Fatalf("Failed to get free port for worker: %v", err)
|
|
}
|
|
|
|
workDir, _ := os.Getwd()
|
|
workerPath := filepath.Join(workDir, "cmd", "facial-rotation-detector", "worker.py")
|
|
|
|
pythonBin := "/home/creserver/extras/local_venv/bin/python3"
|
|
if _, err := os.Stat(pythonBin); os.IsNotExist(err) {
|
|
pythonBin = "python3"
|
|
}
|
|
|
|
cmd := exec.Command(pythonBin, workerPath, "--port", workerPort)
|
|
cmd.Stdout = os.Stdout
|
|
cmd.Stderr = os.Stderr
|
|
|
|
go func() {
|
|
slog.Info("starting Python facial worker", "port", workerPort)
|
|
if err := cmd.Run(); err != nil {
|
|
slog.Error("Python worker exited with error", "error", err)
|
|
}
|
|
}()
|
|
|
|
// Wait for worker to be ready
|
|
for i := 0; i < 30; i++ {
|
|
resp, err := http.Get(fmt.Sprintf("http://127.0.0.1:%s/health", workerPort))
|
|
if err == nil && resp.StatusCode == 200 {
|
|
resp.Body.Close()
|
|
slog.Info("Python facial worker is ready and responding", "port", workerPort)
|
|
return
|
|
}
|
|
time.Sleep(1 * time.Second)
|
|
}
|
|
slog.Error("Python worker failed to start in time")
|
|
log.Fatal("Python worker failed to start in time.")
|
|
}
|
|
|
|
func main() {
|
|
common.InitLogger("facial-rotation-detector")
|
|
initPaths()
|
|
startPythonWorker()
|
|
|
|
port := os.Getenv("PORT")
|
|
if port == "" {
|
|
port = "5007"
|
|
}
|
|
|
|
r := chi.NewRouter()
|
|
r.Use(middleware.RealIP)
|
|
r.Use(middleware.RequestID)
|
|
r.Use(mymiddleware.RequestLogger)
|
|
r.Use(middleware.Recoverer)
|
|
|
|
r.Use(cors.Handler(cors.Options{
|
|
AllowedOrigins: []string{"*"},
|
|
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
|
|
AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token"},
|
|
}))
|
|
|
|
if scriptName := os.Getenv("SCRIPT_NAME"); scriptName != "" {
|
|
r.Use(common.PrefixMiddleware(scriptName))
|
|
}
|
|
|
|
// Static Files
|
|
workDir, _ := os.Getwd()
|
|
webDir := filepath.Join(workDir, "web", "facial-rotation-detector")
|
|
r.Handle("/css/*", http.StripPrefix("/css/", http.FileServer(http.Dir(filepath.Join(webDir, "css")))))
|
|
r.Handle("/js/*", http.StripPrefix("/js/", http.FileServer(http.Dir(filepath.Join(webDir, "js")))))
|
|
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
|
|
http.ServeFile(w, r, filepath.Join(webDir, "index.html"))
|
|
})
|
|
|
|
r.Handle("/uploads/*", http.StripPrefix("/uploads/", http.FileServer(http.Dir(uploadFolder))))
|
|
r.Handle("/processed/*", http.StripPrefix("/processed/", http.FileServer(http.Dir(processedFolder))))
|
|
|
|
r.Get("/health", func(w http.ResponseWriter, r *http.Request) {
|
|
writeJSON(w, 200, map[string]string{"status": "ok"})
|
|
})
|
|
|
|
r.Post("/upload", handleUpload)
|
|
r.Post("/save_coordinates", handleSaveCoordinates)
|
|
r.Post("/save_all_coordinates", handleSaveAllCoordinates)
|
|
r.Get("/get_coordinates", handleGetCoordinates)
|
|
r.Get("/get_json_results", handleGetJSONResults)
|
|
r.Get("/download/json", handleDownloadJSON)
|
|
r.Get("/download/text", handleDownloadText)
|
|
r.Post("/clear_coordinates", handleClearCoordinates)
|
|
|
|
slog.Info("starting facial-rotation-detector", "port", port, "pid", os.Getpid())
|
|
log.Fatal(http.ListenAndServe(":"+port, r))
|
|
}
|
|
|
|
func handleUpload(w http.ResponseWriter, r *http.Request) {
|
|
r.ParseMultipartForm(16 << 20) // 16MB max
|
|
|
|
file, header, err := r.FormFile("file")
|
|
if err != nil {
|
|
writeJSON(w, 400, map[string]any{"success": false, "error": "No file uploaded"})
|
|
return
|
|
}
|
|
defer file.Close()
|
|
|
|
ext := strings.ToLower(filepath.Ext(header.Filename))
|
|
if ext == "" {
|
|
ext = ".png"
|
|
}
|
|
timestamp := time.Now().Unix()
|
|
baseName := strings.TrimSuffix(header.Filename, filepath.Ext(header.Filename))
|
|
filename := fmt.Sprintf("%s_%d%s", baseName, timestamp, ext)
|
|
uploadPath := filepath.Join(uploadFolder, filename)
|
|
|
|
out, err := os.Create(uploadPath)
|
|
if err != nil {
|
|
writeJSON(w, 500, map[string]any{"success": false, "error": "Failed to save file"})
|
|
return
|
|
}
|
|
io.Copy(out, file)
|
|
out.Close()
|
|
|
|
processedFilename := fmt.Sprintf("processed_%s", filename)
|
|
processedPath := filepath.Join(processedFolder, processedFilename)
|
|
|
|
// Call Python HTTP Worker
|
|
payload := map[string]string{"input": uploadPath, "output": processedPath}
|
|
jsonPayload, _ := json.Marshal(payload)
|
|
resp, err := http.Post(fmt.Sprintf("http://127.0.0.1:%s/process", workerPort), "application/json", bytes.NewBuffer(jsonPayload))
|
|
if err != nil {
|
|
writeJSON(w, 500, map[string]any{"success": false, "error": "Failed to contact worker"})
|
|
return
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
var result map[string]any
|
|
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
|
|
writeJSON(w, 500, map[string]any{"success": false, "error": "Invalid output from processor"})
|
|
return
|
|
}
|
|
|
|
if success, ok := result["success"].(bool); ok && success {
|
|
prefix := r.Header.Get("X-Forwarded-Prefix")
|
|
result["image_url"] = fmt.Sprintf("%s/processed/%s", prefix, processedFilename)
|
|
result["original_url"] = fmt.Sprintf("%s/uploads/%s", prefix, filename)
|
|
}
|
|
|
|
writeJSON(w, 200, result)
|
|
}
|
|
|
|
func handleSaveCoordinates(w http.ResponseWriter, r *http.Request) {
|
|
var data map[string]any
|
|
json.NewDecoder(r.Body).Decode(&data)
|
|
|
|
imageName, ok := data["image_name"].(string)
|
|
if !ok || imageName == "" {
|
|
writeJSON(w, 400, map[string]any{"success": false, "error": "Invalid data"})
|
|
return
|
|
}
|
|
|
|
coordStr, _ := data["coordinate_string"].(string)
|
|
rotStr, _ := data["rotation_string"].(string)
|
|
|
|
var angleStr string
|
|
if rects, ok := data["rectangles"].([]any); ok {
|
|
angles := []string{}
|
|
for _, rectAny := range rects {
|
|
if rect, ok := rectAny.(map[string]any); ok {
|
|
if ang, ok := rect["angle"].(float64); ok {
|
|
angles = append(angles, fmt.Sprintf("%v", ang))
|
|
}
|
|
}
|
|
}
|
|
angleStr = strings.Join(angles, ";")
|
|
}
|
|
|
|
|
|
// Save TXT
|
|
entry := fmt.Sprintf("Image: %s\ncoordinate: %s\nrotationpoint: %s\n", imageName, coordStr, rotStr)
|
|
if angleStr != "" {
|
|
entry += fmt.Sprintf("angle: %s\n", angleStr)
|
|
}
|
|
entry += strings.Repeat("-", 50) + "\n"
|
|
|
|
f, _ := os.OpenFile(coordinatesFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
|
f.WriteString(entry)
|
|
f.Close()
|
|
|
|
// Save JSON
|
|
var jsonData []map[string]any
|
|
if b, err := os.ReadFile(resultsJSONFile); err == nil {
|
|
json.Unmarshal(b, &jsonData)
|
|
}
|
|
|
|
jsonEntry := map[string]any{
|
|
"filename": imageName,
|
|
"coordinate": coordStr,
|
|
"rotationpoint": rotStr,
|
|
}
|
|
if angleStr != "" {
|
|
jsonEntry["angle"] = angleStr
|
|
}
|
|
|
|
jsonData = append(jsonData, jsonEntry)
|
|
b, _ := json.MarshalIndent(jsonData, "", " ")
|
|
os.WriteFile(resultsJSONFile, b, 0644)
|
|
|
|
writeJSON(w, 200, map[string]any{"success": true})
|
|
}
|
|
|
|
func handleSaveAllCoordinates(w http.ResponseWriter, r *http.Request) {
|
|
var data struct {
|
|
Results []map[string]any `json:"results"`
|
|
}
|
|
json.NewDecoder(r.Body).Decode(&data)
|
|
|
|
if len(data.Results) == 0 {
|
|
writeJSON(w, 400, map[string]any{"success": false, "error": "Invalid data"})
|
|
return
|
|
}
|
|
|
|
sort.Slice(data.Results, func(i, j int) bool {
|
|
nameI, _ := data.Results[i]["filename"].(string)
|
|
nameJ, _ := data.Results[j]["filename"].(string)
|
|
return naturalLess(nameI, nameJ)
|
|
})
|
|
|
|
timestamp := time.Now().Format("2006-01-02 15:04:05")
|
|
|
|
// Append TXT
|
|
f, _ := os.OpenFile(coordinatesFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
|
sep := strings.Repeat("=", 60)
|
|
f.WriteString(fmt.Sprintf("\n%s\nBATCH PROCESSING RESULTS - %s\nTotal Images: %d\n%s\n\n", sep, timestamp, len(data.Results), sep))
|
|
|
|
for _, item := range data.Results {
|
|
name, _ := item["filename"].(string)
|
|
coords, _ := item["coordinates"].(string)
|
|
rotStr, _ := item["rotation_string"].(string)
|
|
entry := fmt.Sprintf("Image: %s\ncoordinate: %s\nrotationpoint: %s\n", name, coords, rotStr)
|
|
|
|
if angleStr, ok := item["angle_string"].(string); ok && angleStr != "" {
|
|
entry += fmt.Sprintf("angle: %s\n", angleStr)
|
|
}
|
|
entry += strings.Repeat("-", 50) + "\n"
|
|
f.WriteString(entry)
|
|
}
|
|
f.Close()
|
|
|
|
// Replace JSON completely according to app.py?
|
|
// Wait, app.py logic for save_all_coordinates:
|
|
var jsonData []map[string]any
|
|
for _, item := range data.Results {
|
|
name, _ := item["filename"].(string)
|
|
coords, _ := item["coordinates"].(string)
|
|
rotStr, _ := item["rotation_string"].(string)
|
|
jEntry := map[string]any{
|
|
"filename": name,
|
|
"coordinate": coords,
|
|
"rotationpoint": rotStr,
|
|
}
|
|
if angleStr, ok := item["angle_string"].(string); ok && angleStr != "" {
|
|
jEntry["angle"] = angleStr
|
|
}
|
|
jsonData = append(jsonData, jEntry)
|
|
}
|
|
b, _ := json.MarshalIndent(jsonData, "", " ")
|
|
os.WriteFile(resultsJSONFile, b, 0644)
|
|
|
|
writeJSON(w, 200, map[string]any{"success": true})
|
|
}
|
|
|
|
func handleGetCoordinates(w http.ResponseWriter, r *http.Request) {
|
|
content, err := os.ReadFile(coordinatesFile)
|
|
if err != nil {
|
|
writeJSON(w, 200, map[string]any{"content": ""})
|
|
return
|
|
}
|
|
writeJSON(w, 200, map[string]any{"content": string(content)})
|
|
}
|
|
|
|
func handleGetJSONResults(w http.ResponseWriter, r *http.Request) {
|
|
content, err := os.ReadFile(resultsJSONFile)
|
|
if err != nil || len(content) == 0 {
|
|
writeJSON(w, 200, []any{})
|
|
return
|
|
}
|
|
var res []any
|
|
json.Unmarshal(content, &res)
|
|
writeJSON(w, 200, res)
|
|
}
|
|
|
|
func handleDownloadJSON(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Disposition", "attachment; filename=results.json")
|
|
http.ServeFile(w, r, resultsJSONFile)
|
|
}
|
|
|
|
func handleDownloadText(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Disposition", "attachment; filename=coordinates.txt")
|
|
http.ServeFile(w, r, coordinatesFile)
|
|
}
|
|
|
|
func handleClearCoordinates(w http.ResponseWriter, r *http.Request) {
|
|
os.WriteFile(coordinatesFile, []byte(""), 0644)
|
|
os.WriteFile(resultsJSONFile, []byte("[]"), 0644)
|
|
writeJSON(w, 200, map[string]any{"success": true})
|
|
}
|
|
|
|
func writeJSON(w http.ResponseWriter, status int, data any) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(status)
|
|
json.NewEncoder(w).Encode(data)
|
|
}
|
|
|
|
// naturalLess implements natural string sorting (e.g. img2 < img10)
|
|
func naturalLess(a, b string) bool {
|
|
re := regexp.MustCompile(`\d+|\D+`)
|
|
aParts := re.FindAllString(a, -1)
|
|
bParts := re.FindAllString(b, -1)
|
|
|
|
for i := 0; i < len(aParts) && i < len(bParts); i++ {
|
|
if aParts[i] != bParts[i] {
|
|
aInt, aErr := strconv.Atoi(aParts[i])
|
|
bInt, bErr := strconv.Atoi(bParts[i])
|
|
if aErr == nil && bErr == nil {
|
|
return aInt < bInt
|
|
}
|
|
return aParts[i] < bParts[i]
|
|
}
|
|
}
|
|
return len(aParts) < len(bParts)
|
|
}
|
|
|