AdityaAdaki commited on
Commit
9afb6a2
·
1 Parent(s): 1265a74
Files changed (4) hide show
  1. main.py +51 -5
  2. static/css/styles.css +0 -44
  3. static/temp_map.html +72 -0
  4. templates/index.html +1 -1
main.py CHANGED
@@ -253,8 +253,15 @@ def setup_webdriver():
253
  chrome_options.add_argument('--headless')
254
  chrome_options.add_argument('--no-sandbox')
255
  chrome_options.add_argument('--disable-dev-shm-usage')
256
- chrome_options.binary_location = os.getenv('CHROME_BINARY_LOCATION', '/usr/bin/google-chrome')
257
- return webdriver.Chrome(options=chrome_options)
 
 
 
 
 
 
 
258
 
259
  def create_polygon_mask(image_size, points):
260
  """Create a mask image from polygon points"""
@@ -336,14 +343,34 @@ def capture_screenshot():
336
 
337
  time.sleep(1)
338
 
339
- driver = setup_webdriver()
 
 
 
 
 
 
 
 
 
 
 
 
340
  try:
341
  driver.set_window_size(width + 50, height + 50)
342
  map_url = f"http://localhost:{PORT}/static/temp_map.html"
343
  driver.get(map_url)
344
  time.sleep(3)
 
 
 
 
 
345
  driver.save_screenshot(filepath)
346
 
 
 
 
347
  if polygon_points and len(polygon_points) >= 3:
348
  img = Image.open(filepath)
349
  mask = create_polygon_mask(img.size, polygon_points)
@@ -352,6 +379,10 @@ def capture_screenshot():
352
  cutout_filename = f"cutout_{datetime.now().strftime('%Y%m%d_%H%M%S')}.png"
353
  cutout_filepath = os.path.join(SCREENSHOT_DIR, cutout_filename)
354
  cutout.save(cutout_filepath)
 
 
 
 
355
  return jsonify({
356
  'success': True,
357
  'screenshot_path': f'/static/screenshots/{filename}',
@@ -363,12 +394,27 @@ def capture_screenshot():
363
  'screenshot_path': f'/static/screenshots/{filename}'
364
  })
365
 
 
 
 
 
 
 
 
 
 
 
366
  finally:
367
- driver.quit()
 
 
 
368
 
369
  except Exception as e:
370
  logging.error(f"Screenshot error: {str(e)}")
371
- return jsonify({'error': str(e)}), 500
 
 
372
 
373
  @app.route('/analyze')
374
  def analyze():
 
253
  chrome_options.add_argument('--headless')
254
  chrome_options.add_argument('--no-sandbox')
255
  chrome_options.add_argument('--disable-dev-shm-usage')
256
+
257
+ # Check if running on Windows or Linux
258
+ if os.name == 'nt': # Windows
259
+ # Let Selenium Manager handle driver installation
260
+ chrome_options.binary_location = None # Use default Chrome installation
261
+ return webdriver.Chrome(options=chrome_options)
262
+ else: # Linux
263
+ chrome_options.binary_location = os.getenv('CHROME_BINARY_LOCATION', '/usr/bin/google-chrome')
264
+ return webdriver.Chrome(options=chrome_options)
265
 
266
  def create_polygon_mask(image_size, points):
267
  """Create a mask image from polygon points"""
 
343
 
344
  time.sleep(1)
345
 
346
+ try:
347
+ driver = setup_webdriver()
348
+ except Exception as e:
349
+ logging.error(f"Webdriver setup error: {str(e)}")
350
+ error_msg = str(e)
351
+ if "chromedriver" in error_msg.lower():
352
+ return jsonify({
353
+ 'error': 'ChromeDriver setup failed. Please ensure Chrome is installed.'
354
+ }), 500
355
+ return jsonify({
356
+ 'error': 'Failed to initialize screenshot capture. Please try again.'
357
+ }), 500
358
+
359
  try:
360
  driver.set_window_size(width + 50, height + 50)
361
  map_url = f"http://localhost:{PORT}/static/temp_map.html"
362
  driver.get(map_url)
363
  time.sleep(3)
364
+
365
+ # Check if the map loaded properly
366
+ if not os.path.exists(map_path):
367
+ raise Exception("Map file not generated")
368
+
369
  driver.save_screenshot(filepath)
370
 
371
+ if not os.path.exists(filepath):
372
+ raise Exception("Screenshot not saved")
373
+
374
  if polygon_points and len(polygon_points) >= 3:
375
  img = Image.open(filepath)
376
  mask = create_polygon_mask(img.size, polygon_points)
 
379
  cutout_filename = f"cutout_{datetime.now().strftime('%Y%m%d_%H%M%S')}.png"
380
  cutout_filepath = os.path.join(SCREENSHOT_DIR, cutout_filename)
381
  cutout.save(cutout_filepath)
382
+
383
+ if not os.path.exists(cutout_filepath):
384
+ raise Exception("Cutout not saved")
385
+
386
  return jsonify({
387
  'success': True,
388
  'screenshot_path': f'/static/screenshots/{filename}',
 
394
  'screenshot_path': f'/static/screenshots/{filename}'
395
  })
396
 
397
+ except Exception as e:
398
+ logging.error(f"Screenshot capture error: {str(e)}")
399
+ error_msg = str(e)
400
+ if "timeout" in error_msg.lower():
401
+ return jsonify({
402
+ 'error': 'Map loading timed out. Please try again.'
403
+ }), 500
404
+ return jsonify({
405
+ 'error': 'Failed to capture screenshot. Please try again.'
406
+ }), 500
407
  finally:
408
+ try:
409
+ driver.quit()
410
+ except:
411
+ pass
412
 
413
  except Exception as e:
414
  logging.error(f"Screenshot error: {str(e)}")
415
+ return jsonify({
416
+ 'error': 'An unexpected error occurred. Please try again.'
417
+ }), 500
418
 
419
  @app.route('/analyze')
420
  def analyze():
static/css/styles.css DELETED
@@ -1,44 +0,0 @@
1
- #map {
2
- position: relative;
3
- width: 100%;
4
- height: 500px;
5
- overflow: hidden;
6
- }
7
-
8
- .draw-button {
9
- position: absolute;
10
- top: 10px;
11
- right: 10px;
12
- z-index: 1000;
13
- padding: 8px 16px;
14
- background-color: #fff;
15
- border: 2px solid #ccc;
16
- border-radius: 4px;
17
- cursor: pointer;
18
- }
19
-
20
- .draw-button:hover {
21
- background-color: #f0f0f0;
22
- }
23
-
24
- /* Make sure the SVG overlay is visible and blocks map interaction during drawing */
25
- svg {
26
- position: absolute;
27
- top: 0;
28
- left: 0;
29
- width: 100%;
30
- height: 100%;
31
- z-index: 1000;
32
- }
33
-
34
- /* Style for the polygon being drawn */
35
- polygon {
36
- fill: rgba(255, 0, 0, 0.2);
37
- stroke: red;
38
- stroke-width: 2;
39
- }
40
-
41
- /* Style for the map container during drawing */
42
- #map.drawing-mode iframe {
43
- pointer-events: none;
44
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
static/temp_map.html ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+
5
+ <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
6
+
7
+ <script>
8
+ L_NO_TOUCH = false;
9
+ L_DISABLE_3D = false;
10
+ </script>
11
+
12
+ <style>html, body {width: 100%;height: 100%;margin: 0;padding: 0;}</style>
13
+ <style>#map {position:absolute;top:0;bottom:0;right:0;left:0;}</style>
14
+ <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/leaflet.js"></script>
15
+ <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
16
+ <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
17
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/Leaflet.awesome-markers/2.0.2/leaflet.awesome-markers.js"></script>
18
+ <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/leaflet.css"/>
19
+ <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"/>
20
+ <link rel="stylesheet" href="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap-glyphicons.css"/>
21
+ <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/[email protected]/css/all.min.css"/>
22
+ <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/Leaflet.awesome-markers/2.0.2/leaflet.awesome-markers.css"/>
23
+ <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/python-visualization/folium/folium/templates/leaflet.awesome.rotate.min.css"/>
24
+
25
+ <meta name="viewport" content="width=device-width,
26
+ initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
27
+ <style>
28
+ #map_523d0a34c54581e390ad502b0b1160a9 {
29
+ position: relative;
30
+ width: 969.0px;
31
+ height: 500.0px;
32
+ left: 0.0%;
33
+ top: 0.0%;
34
+ }
35
+ .leaflet-container { font-size: 1rem; }
36
+ </style>
37
+
38
+ </head>
39
+ <body>
40
+
41
+
42
+ <div class="folium-map" id="map_523d0a34c54581e390ad502b0b1160a9" ></div>
43
+
44
+ </body>
45
+ <script>
46
+
47
+
48
+ var map_523d0a34c54581e390ad502b0b1160a9 = L.map(
49
+ "map_523d0a34c54581e390ad502b0b1160a9",
50
+ {
51
+ center: [18.463836119599378, 73.86806580367184],
52
+ crs: L.CRS.EPSG3857,
53
+ zoom: 18,
54
+ zoomControl: true,
55
+ preferCanvas: false,
56
+ }
57
+ );
58
+
59
+
60
+
61
+
62
+
63
+ var tile_layer_ab287d9f0977d3570f0f8ea43ca86459 = L.tileLayer(
64
+ "https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}",
65
+ {"attribution": "Esri", "detectRetina": false, "maxZoom": 18, "minZoom": 0, "noWrap": false, "opacity": 1, "subdomains": "abc", "tms": false}
66
+ );
67
+
68
+
69
+ tile_layer_ab287d9f0977d3570f0f8ea43ca86459.addTo(map_523d0a34c54581e390ad502b0b1160a9);
70
+
71
+ </script>
72
+ </html>
templates/index.html CHANGED
@@ -1,5 +1,5 @@
1
  <!DOCTYPE html>
2
- <html>
3
  <head>
4
  <meta charset="UTF-8">
5
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
 
1
  <!DOCTYPE html>
2
+ <html lang="en">
3
  <head>
4
  <meta charset="UTF-8">
5
  <meta name="viewport" content="width=device-width, initial-scale=1.0">