MySafeCode commited on
Commit
69d3518
·
verified ·
1 Parent(s): e7de881

Upload 4 files

Browse files
Files changed (4) hide show
  1. Dockerfile +11 -0
  2. README.md +5 -10
  3. app.py +59 -0
  4. requirements.txt +2 -0
Dockerfile ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.11-slim-bullseye
2
+ RUN apt-get update && apt-get install -y libsdl2-2.0-0 libfreetype6 xvfb
3
+ WORKDIR /app
4
+ COPY requirements.txt .
5
+ RUN pip install --no-cache-dir -r requirements.txt
6
+ COPY app.py .
7
+ ENV SDL_VIDEODRIVER=dummy
8
+ ENV PYGAME_HIDE_SUPPORT_PROMPT=1
9
+ ENV DISPLAY=:99
10
+ EXPOSE 7860
11
+ CMD ["python", "app.py"]
README.md CHANGED
@@ -1,10 +1,5 @@
1
- ---
2
- title: Flaskdockernew
3
- emoji: 👁
4
- colorFrom: yellow
5
- colorTo: indigo
6
- sdk: docker
7
- pinned: false
8
- ---
9
-
10
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
+ ---
2
+ title: PyGame Stream
3
+ sdk: docker
4
+ app_file: app.py
5
+ ---
 
 
 
 
 
app.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pygame, time, threading, base64, os
2
+ from flask import Flask, Response, render_template_string
3
+
4
+ os.environ['SDL_VIDEODRIVER'] = 'dummy'
5
+ pygame.init()
6
+
7
+ WIDTH, HEIGHT = 800, 600
8
+ shared = {"latest_frame": None, "frame_count": 0, "streaming": True}
9
+
10
+ def capture_loop():
11
+ screen = pygame.Surface((WIDTH, HEIGHT))
12
+ circle_x, circle_y = WIDTH//2, HEIGHT//2
13
+ speed_x, speed_y = 4, 3
14
+
15
+ while shared["streaming"]:
16
+ start_time = time.time()
17
+ circle_x += speed_x
18
+ circle_y += speed_y
19
+ if circle_x < 30 or circle_x > WIDTH-30: speed_x *= -1
20
+ if circle_y < 30 or circle_y > HEIGHT-30: speed_y *= -1
21
+
22
+ screen.fill((25, 25, 45))
23
+ pygame.draw.circle(screen, (255, 80, 80), (int(circle_x), int(circle_y)), 30)
24
+ frame_str = pygame.image.tostring(screen, 'RGB')
25
+ shared["latest_frame"] = base64.b64encode(frame_str).decode('utf-8')
26
+ shared["frame_count"] += 1
27
+
28
+ elapsed = time.time() - start_time
29
+ if elapsed < 1.0/30: time.sleep(1.0/30 - elapsed)
30
+
31
+ app = Flask(__name__)
32
+ HTML = '''<html><body style="background:#0f172a;color:white">
33
+ <h1>PyGame Stream</h1>
34
+ <img id="streamImg" src="/stream" width="400" height="300">
35
+ <p>Frames: <span id="count">0</span></p>
36
+ <script>setInterval(()=>{
37
+ document.getElementById('streamImg').src='/stream?_='+Date.now();
38
+ fetch('/stats').then(r=>r.json()).then(d=>{
39
+ document.getElementById('count').textContent=d.frame_count;
40
+ });},33);</script></body></html>'''
41
+
42
+ @app.route('/')
43
+ def index(): return render_template_string(HTML)
44
+
45
+ @app.route('/stats')
46
+ def stats(): return {'frame_count': shared['frame_count']}
47
+
48
+ @app.route('/stream')
49
+ def stream():
50
+ if shared['latest_frame']:
51
+ return Response(base64.b64decode(shared['latest_frame']), mimetype='image/x-rgb')
52
+ return Response(b'', mimetype='image/jpeg')
53
+
54
+ if __name__ == "__main__":
55
+ threading.Thread(target=capture_loop, daemon=True).start()
56
+ for _ in range(50):
57
+ if shared['latest_frame']: break
58
+ time.sleep(0.1)
59
+ app.run(host='0.0.0.0', port=int(os.environ.get('PORT', 7860)), debug=False, threaded=True)
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ flask==2.3.3
2
+ pygame==2.5.2