Akwbw commited on
Commit
b60127b
·
verified ·
1 Parent(s): 841b576

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -11
app.py CHANGED
@@ -8,6 +8,7 @@ import base64
8
  from flask import Flask, request, jsonify
9
  from flask_cors import CORS
10
  import pyautogui
 
11
  from PIL import Image
12
  import io
13
 
@@ -17,32 +18,38 @@ CORS(app)
17
  SCREEN_WIDTH = 1024
18
  SCREEN_HEIGHT = 768
19
 
20
- # Fail-safes disable karo taake mouse corner mein jane se app band na ho
21
  pyautogui.FAILSAFE = False
22
 
 
 
 
 
 
23
  @app.route('/')
24
  def home():
25
- return "Cloud PC is Running! Use HTML Client."
26
 
27
  @app.route('/snapshot', methods=['GET'])
28
  def get_snapshot():
29
  try:
30
- # METHOD CHANGE: mss ki jagah pyautogui use kar rahe hain (More Stable)
31
- # Yeh seedha screen capture karega
32
- img = pyautogui.screenshot()
 
 
33
 
34
- # Resize kar rahe hain taake mobile par tez chale
35
- img.thumbnail((600, 600))
36
 
37
  buffer = io.BytesIO()
38
- img.save(buffer, format="JPEG", quality=50)
39
  b64 = base64.b64encode(buffer.getvalue()).decode('utf-8')
40
 
41
  return jsonify({"screenshot": b64})
42
  except Exception as e:
43
- # Error console mein print hoga
44
- print(f"ERROR: {e}")
45
- # Agar error aye to client ko batao (Black screen ki jagah error dikhega)
46
  return jsonify({"error": str(e)}), 500
47
 
48
  @app.route('/interact', methods=['POST'])
@@ -75,7 +82,9 @@ def interact():
75
 
76
  return jsonify({"status": "ok"})
77
  except Exception as e:
 
78
  return jsonify({"error": str(e)}), 500
79
 
80
  if __name__ == '__main__':
 
81
  app.run(host='0.0.0.0', port=7860, threaded=True)
 
8
  from flask import Flask, request, jsonify
9
  from flask_cors import CORS
10
  import pyautogui
11
+ import mss
12
  from PIL import Image
13
  import io
14
 
 
18
  SCREEN_WIDTH = 1024
19
  SCREEN_HEIGHT = 768
20
 
21
+ # Fail-safes disable
22
  pyautogui.FAILSAFE = False
23
 
24
+ # MSS (Fast Screenshot) Setup
25
+ sct = mss.mss()
26
+ # Monitor 1 ko pakdo
27
+ monitor = sct.monitors[1]
28
+
29
  @app.route('/')
30
  def home():
31
+ return "Cloud PC Ready. Connect via HTML."
32
 
33
  @app.route('/snapshot', methods=['GET'])
34
  def get_snapshot():
35
  try:
36
+ # MSS se screenshot lo (Yeh Scrot/DBus use nahi karta, direct memory read karta hai)
37
+ sct_img = sct.grab(monitor)
38
+
39
+ # Raw bytes ko Image mein convert karo
40
+ img = Image.frombytes("RGB", sct_img.size, sct_img.bgra, "raw", "BGRX")
41
 
42
+ # Resize for speed (Mobile data bachane ke liye)
43
+ img.thumbnail((600, 600))
44
 
45
  buffer = io.BytesIO()
46
+ img.save(buffer, format="JPEG", quality=60)
47
  b64 = base64.b64encode(buffer.getvalue()).decode('utf-8')
48
 
49
  return jsonify({"screenshot": b64})
50
  except Exception as e:
51
+ # AB AGAR ERROR AAYA TOH LOGS MEIN DIKHEGA
52
+ print(f"!!! SNAPSHOT ERROR !!!: {e}")
 
53
  return jsonify({"error": str(e)}), 500
54
 
55
  @app.route('/interact', methods=['POST'])
 
82
 
83
  return jsonify({"status": "ok"})
84
  except Exception as e:
85
+ print(f"INTERACT ERROR: {e}")
86
  return jsonify({"error": str(e)}), 500
87
 
88
  if __name__ == '__main__':
89
+ # Threaded=False kabhi kabhi crash rokta hai
90
  app.run(host='0.0.0.0', port=7860, threaded=True)