ParthivM1 commited on
Commit
96ad65a
·
1 Parent(s): 4cb00f4

fix: Update requirements.txt

Browse files
__pycache__/app.cpython-311.pyc ADDED
Binary file (6.24 kB). View file
 
__pycache__/app.cpython-313.pyc ADDED
Binary file (5.62 kB). View file
 
app.py CHANGED
@@ -4,7 +4,8 @@ import uuid
4
  from fractions import Fraction
5
  import exifread
6
  import uvicorn
7
- from fastapi import FastAPI, UploadFile, File, HTTPException
 
8
  from fastapi.middleware.cors import CORSMiddleware
9
  from dotenv import load_dotenv
10
  from ultralytics import YOLO
@@ -24,23 +25,22 @@ app.add_middleware(
24
  allow_credentials=True,
25
  allow_methods=["*"],
26
  allow_headers=["*"]
27
- )
28
 
29
  SUPABASE_URL = os.getenv("SUPABASE_URL")
30
- SUPABASE_KEY = os.getenv("SUPABASE_KEY")
31
 
32
  if not SUPABASE_URL or not SUPABASE_KEY:
33
  raise ValueError("Supabase credentials not found. Please set them in your environment variables.")
34
-
35
  supabase: Client = create_client(SUPABASE_URL, SUPABASE_KEY)
36
 
37
- #Helper Functions
38
-
39
  def convert_to_decimal(coord):
40
- deg = float(coord.values[0])
41
- min = float(coord.values[1]) / 60
42
- sec = float(Fraction(str(coord.values[2]))) / 3600
43
- return deg + min + sec
 
 
44
 
45
  def get_gps_location(file_stream):
46
  tags = exifread.process_file(file_stream)
@@ -57,22 +57,40 @@ def get_gps_location(file_stream):
57
  return lat, lon
58
  return None, None
59
 
 
 
 
 
 
 
 
60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
 
62
- @app.post("/upload")
63
-
64
- async def upload_image(file: UploadFile = File(...)):
65
 
 
 
 
66
  image_data = await file.read()
67
 
68
  with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as temp_image_file:
69
  temp_image_file.write(image_data)
70
  temp_path = temp_image_file.name
71
 
72
- results = model(temp_path)
73
-
74
- annotated_image_array = results[0].plot()
75
-
76
  os.remove(temp_path)
77
 
78
  is_success, buffer = cv2.imencode(".jpg", annotated_image_array)
@@ -82,33 +100,38 @@ async def upload_image(file: UploadFile = File(...)):
82
  annotated_image_bytes = buffer.tobytes()
83
 
84
  bucket_name = 'pothole-images'
85
- file_extension = os.path.splitext(file.filename)[1]
86
- uuid_path = f"{uuid.uuid4()}{file_extension}"
87
 
88
  try:
89
  supabase.storage.from_(bucket_name).upload(
90
  path=uuid_path,
91
  file=annotated_image_bytes,
92
- file_options={"content-type": file.content_type}
93
  )
94
-
95
  url = supabase.storage.from_(bucket_name).get_public_url(uuid_path)
96
  except Exception as e:
97
  raise HTTPException(status_code=500, detail=f"Storage Error: {str(e)}")
98
 
99
-
100
  lat, lon = get_gps_location(file.file)
101
-
102
- if lat and lon:
103
- location_point = f"Point({lon:.6f} {lat:.6f})"
104
- else:
105
- location_point = None
106
-
107
 
108
  try:
 
 
 
 
 
 
 
 
 
 
 
109
  response = (
110
  supabase.table("potholes")
111
- .insert({"image_name": file.filename, "location": location_point, "image_url": url})
112
  .execute()
113
  )
114
  except Exception as e:
@@ -117,4 +140,4 @@ async def upload_image(file: UploadFile = File(...)):
117
  return {"message": "Upload successful", "image_url": url, "db_response": response.data}
118
 
119
  if __name__ == "__main__":
120
- uvicorn.run(app, port=8000, host="127.0.0.1")
 
4
  from fractions import Fraction
5
  import exifread
6
  import uvicorn
7
+ # ** FIX: Import Form **
8
+ from fastapi import FastAPI, UploadFile, File, HTTPException, Form
9
  from fastapi.middleware.cors import CORSMiddleware
10
  from dotenv import load_dotenv
11
  from ultralytics import YOLO
 
25
  allow_credentials=True,
26
  allow_methods=["*"],
27
  allow_headers=["*"]
28
+ )
29
 
30
  SUPABASE_URL = os.getenv("SUPABASE_URL")
31
+ SUPABASE_KEY = os.getenv("SUPABASE_KEY")
32
 
33
  if not SUPABASE_URL or not SUPABASE_KEY:
34
  raise ValueError("Supabase credentials not found. Please set them in your environment variables.")
 
35
  supabase: Client = create_client(SUPABASE_URL, SUPABASE_KEY)
36
 
 
 
37
  def convert_to_decimal(coord):
38
+ if coord and len(coord.values) >= 3:
39
+ deg = float(coord.values[0])
40
+ min = float(coord.values[1]) / 60
41
+ sec = float(Fraction(str(coord.values[2]))) / 3600
42
+ return deg + min + sec
43
+ return 0.0
44
 
45
  def get_gps_location(file_stream):
46
  tags = exifread.process_file(file_stream)
 
57
  return lat, lon
58
  return None, None
59
 
60
+ @app.get("/reports")
61
+ async def get_reports():
62
+ try:
63
+ response = supabase.table("potholes").select("id, guid, image_name, image_url").order("created_at", desc=True).limit(30).execute()
64
+ return response.data
65
+ except Exception as e:
66
+ raise HTTPException(status_code=500, detail=f"Database Error: {str(e)}")
67
 
68
+ @app.get("/report/{guid}")
69
+ async def get_report(guid: str):
70
+ try:
71
+ # ** FIX: Select the new location_text column **
72
+ response = supabase.table("potholes").select("guid, image_name, image_url, location, created_at, location_text").eq("guid", guid).execute()
73
+
74
+ if response.data and len(response.data) > 0:
75
+ return response.data[0]
76
+ else:
77
+ raise HTTPException(status_code=404, detail=f"Report with guid {guid} not found.")
78
+
79
+ except Exception as e:
80
+ raise HTTPException(status_code=500, detail=f"Database Error: {str(e)}")
81
 
 
 
 
82
 
83
+ # ** FIX: Accept location text from the form **
84
+ @app.post("/upload")
85
+ async def upload_image(file: UploadFile = File(...), location: str = Form(None)):
86
  image_data = await file.read()
87
 
88
  with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as temp_image_file:
89
  temp_image_file.write(image_data)
90
  temp_path = temp_image_file.name
91
 
92
+ results = model(temp_path, device='cpu')
93
+ annotated_image_array = results[0].plot()
 
 
94
  os.remove(temp_path)
95
 
96
  is_success, buffer = cv2.imencode(".jpg", annotated_image_array)
 
100
  annotated_image_bytes = buffer.tobytes()
101
 
102
  bucket_name = 'pothole-images'
103
+ uuid_path = f"annotated_{uuid.uuid4()}.jpg"
 
104
 
105
  try:
106
  supabase.storage.from_(bucket_name).upload(
107
  path=uuid_path,
108
  file=annotated_image_bytes,
109
+ file_options={"content-type": "image/jpeg"}
110
  )
 
111
  url = supabase.storage.from_(bucket_name).get_public_url(uuid_path)
112
  except Exception as e:
113
  raise HTTPException(status_code=500, detail=f"Storage Error: {str(e)}")
114
 
115
+ await file.seek(0)
116
  lat, lon = get_gps_location(file.file)
117
+ location_point = f"Point({lon:.6f} {lat:.6f})" if lat and lon else None
118
+ guid = str(uuid.uuid4())
 
 
 
 
119
 
120
  try:
121
+ # ** FIX: Add logic to insert either GPS location or text location **
122
+ insert_data = {
123
+ "guid": guid,
124
+ "image_name": file.filename,
125
+ "image_url": url,
126
+ "location": location_point,
127
+ "location_text": None
128
+ }
129
+ if not location_point and location:
130
+ insert_data["location_text"] = location
131
+
132
  response = (
133
  supabase.table("potholes")
134
+ .insert(insert_data)
135
  .execute()
136
  )
137
  except Exception as e:
 
140
  return {"message": "Upload successful", "image_url": url, "db_response": response.data}
141
 
142
  if __name__ == "__main__":
143
+ uvicorn.run(app, port=8000, host="127.0.0.1")
requirements.txt CHANGED
@@ -1,12 +1,8 @@
1
  fastapi
2
  uvicorn
3
- gunicorn
4
- python-dotenv
5
  supabase
6
- exifread
7
  ultralytics
8
- torch
9
- torchvision
10
- python-multipart
11
  opencv-python-headless
12
- jinja2
 
 
1
  fastapi
2
  uvicorn
 
 
3
  supabase
4
+ python-dotenv
5
  ultralytics
 
 
 
6
  opencv-python-headless
7
+ exifread
8
+ numpy