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