Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,40 +1,84 @@
|
|
1 |
-
from flask import Flask, render_template, request, jsonify
|
2 |
-
import os
|
3 |
import cv2
|
4 |
-
import
|
5 |
-
import
|
|
|
|
|
|
|
|
|
6 |
|
7 |
app = Flask(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
-
|
10 |
-
|
11 |
-
|
|
|
|
|
12 |
|
13 |
@app.route('/')
|
14 |
def index():
|
15 |
return render_template('index.html')
|
16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
@app.route('/capture', methods=['POST'])
|
18 |
def capture():
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
return jsonify({
|
26 |
-
|
27 |
-
@app.route('/post_to_instagram', methods=['POST'])
|
28 |
-
def post_to_instagram():
|
29 |
-
# Assuming you're using instabot for posting to Instagram
|
30 |
-
from instabot import Bot
|
31 |
-
bot = Bot()
|
32 |
-
bot.login(username=INSTAGRAM_USERNAME, password=INSTAGRAM_PASSWORD)
|
33 |
-
image_path = "static/images/captured_image.jpg"
|
34 |
-
bot.upload_photo(image_path, caption="Your Image Caption Here")
|
35 |
-
|
36 |
-
return jsonify({"message": "Image posted to Instagram!"})
|
37 |
|
38 |
-
|
39 |
-
|
|
|
|
|
|
|
|
|
|
|
40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, render_template, Response, request, jsonify
|
|
|
2 |
import cv2
|
3 |
+
from camera import VideoCamera
|
4 |
+
from transformers import CLIPProcessor, CLIPModel
|
5 |
+
from PIL import Image
|
6 |
+
import numpy as np
|
7 |
+
from instagrapi import Client
|
8 |
+
import os
|
9 |
|
10 |
app = Flask(__name__)
|
11 |
+
app.config['UPLOAD_FOLDER'] = 'uploads'
|
12 |
+
camera = None
|
13 |
+
captured_image = None
|
14 |
+
|
15 |
+
# Initialize Hugging Face CLIP model
|
16 |
+
model = CLIPModel.from_pretrained("openai/clip-vit-base-patch16")
|
17 |
+
processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch16")
|
18 |
|
19 |
+
def gen(camera):
|
20 |
+
while True:
|
21 |
+
frame = camera.get_frame()
|
22 |
+
yield (b'--frame\r\n'
|
23 |
+
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n')
|
24 |
|
25 |
@app.route('/')
|
26 |
def index():
|
27 |
return render_template('index.html')
|
28 |
|
29 |
+
@app.route('/video_feed')
|
30 |
+
def video_feed():
|
31 |
+
global camera
|
32 |
+
if camera is None:
|
33 |
+
camera = VideoCamera()
|
34 |
+
return Response(gen(camera), mimetype='multipart/x-mixed-replace; boundary=frame')
|
35 |
+
|
36 |
@app.route('/capture', methods=['POST'])
|
37 |
def capture():
|
38 |
+
global captured_image, camera
|
39 |
+
if camera:
|
40 |
+
captured_image = camera.get_frame()
|
41 |
+
img_array = np.frombuffer(captured_image, np.uint8)
|
42 |
+
img = cv2.imdecode(img_array, cv2.IMREAD_COLOR)
|
43 |
+
cv2.imwrite(os.path.join(app.config['UPLOAD_FOLDER'], 'captured.jpg'), img)
|
44 |
+
return jsonify({'status': 'captured'})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
|
46 |
+
@app.route('/switch_camera', methods=['POST'])
|
47 |
+
def switch_camera():
|
48 |
+
global camera
|
49 |
+
camera_type = request.json.get('camera_type', 'user') # 'user' for front, 'environment' for back
|
50 |
+
if camera:
|
51 |
+
camera.switch_camera(camera_type)
|
52 |
+
return jsonify({'status': 'switched'})
|
53 |
|
54 |
+
@app.route('/retake', methods=['POST'])
|
55 |
+
def retake():
|
56 |
+
global captured_image
|
57 |
+
captured_image = None
|
58 |
+
return jsonify({'status': 'retake'})
|
59 |
+
|
60 |
+
@app.route('/upload', methods=['POST'])
|
61 |
+
def upload():
|
62 |
+
global captured_image
|
63 |
+
if captured_image:
|
64 |
+
# Process image with Hugging Face CLIP
|
65 |
+
img_path = os.path.join(app.config['UPLOAD_FOLDER'], 'captured.jpg')
|
66 |
+
image = Image.open(img_path)
|
67 |
+
inputs = processor(images=image, return_tensors="pt")
|
68 |
+
outputs = model.get_image_features(**inputs)
|
69 |
+
# Generate a caption (simplified example)
|
70 |
+
caption = "Captured image from Flask app! #AI #HuggingFace"
|
71 |
+
|
72 |
+
# Upload to Instagram
|
73 |
+
try:
|
74 |
+
cl = Client()
|
75 |
+
cl.login('YOUR_INSTAGRAM_USERNAME', 'YOUR_INSTAGRAM_PASSWORD')
|
76 |
+
cl.photo_upload(img_path, caption=caption)
|
77 |
+
return jsonify({'status': 'uploaded'})
|
78 |
+
except Exception as e:
|
79 |
+
return jsonify({'status': 'error', 'message': str(e)})
|
80 |
+
return jsonify({'status': 'error', 'message': 'No image captured'})
|
81 |
+
|
82 |
+
if __name__ == '__main__':
|
83 |
+
os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
|
84 |
+
app.run(debug=True, host='0.0.0.0', port=5000)
|