Spaces:
Sleeping
Sleeping
| 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') | |
| def index(): | |
| return render_template('index.html') | |
| def video_feed(): | |
| global camera | |
| if camera is None: | |
| camera = VideoCamera() | |
| return Response(gen(camera), mimetype='multipart/x-mixed-replace; boundary=frame') | |
| 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'}) | |
| 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'}) | |
| def retake(): | |
| global captured_image | |
| captured_image = None | |
| return jsonify({'status': 'retake'}) | |
| 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) |