-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
58 lines (48 loc) · 1.56 KB
/
app.py
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
from flask import Flask, request, jsonify
import firebase_admin
from firebase_admin import credentials, messaging
import imgbbpy
import base64
import os
from dotenv import load_dotenv
from io import BytesIO
from PIL import Image
app = Flask(__name__)
load_dotenv()
# Initialize Firebase Admin SDK
cred = credentials.Certificate("key.json")
firebase_admin.initialize_app(cred)
client = imgbbpy.SyncClient(os.getenv('IMGBB_API_KEY'))
def send_push(title, msg, frame_url):
message = messaging.Message(
notification=messaging.Notification(
title=title,
body=msg
),
android=messaging.AndroidConfig(
notification=messaging.AndroidNotification(
image=frame_url
)
),
topic='fire-alert'
)
response = messaging.send(message)
print('Successfully sent message:', response)
@app.route('/send-alert', methods=['POST'])
def send_alert():
data = request.json
image_data = data.get('image_data')
if not image_data:
return jsonify({"error": "No image data provided"}), 400
# Decode the base64 image
image = Image.open(BytesIO(base64.b64decode(image_data)))
image_path = "frame0.jpg"
image.save(image_path)
# Upload the image to imgbb
response = client.upload(file=image_path)
frame_url = response.url
# Send push notification
send_push("FIRE ALERT 🔥🔥", "Fire detected in the CCTV camera", frame_url)
return jsonify({"status": "Alert sent", "frame_url": frame_url}), 200
if __name__ == '__main__':
app.run(debug=True)