Kazel commited on
Commit
df97b0c
·
verified ·
1 Parent(s): 31a9cb1

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -0
app.py ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import cv2
3
+ import ollama
4
+ import threading
5
+
6
+ # Initialize the webcam
7
+ cap = cv2.VideoCapture(0)
8
+
9
+ def query_the_image(query: str, image_list: list[str]):
10
+ try:
11
+ res = ollama.chat(
12
+ model='llava',
13
+ options={
14
+ 'temperature': 0,
15
+ "top_k": 1,
16
+ 'top_p': 0.1,
17
+ 'mirostat_tau': 1.0,
18
+ 'num_ctx': 1024,
19
+ 'seed': 42,
20
+ 'num_predict': 128
21
+ },
22
+ messages=[
23
+ {
24
+ 'role': 'system',
25
+ 'content': "You are a home surveillance system. Answer with very short sentences."
26
+ },
27
+ {
28
+ 'role': 'user',
29
+ 'content': query,
30
+ 'images': image_list,
31
+ }
32
+ ]
33
+ )
34
+ return res['message']['content']
35
+ except Exception as e:
36
+ return f"Error: {e}"
37
+
38
+ def get_frame():
39
+ ret, frame = cap.read()
40
+ if not ret:
41
+ return None
42
+ _, buffer = cv2.imencode('.jpg', frame)
43
+ return buffer.tobytes()
44
+
45
+ def process_image(prompt):
46
+ frame_data = get_frame()
47
+ if frame_data:
48
+ return query_the_image(prompt, [frame_data])
49
+ return "Error capturing image"
50
+
51
+ def video_feed():
52
+ while True:
53
+ ret, frame = cap.read()
54
+ if ret:
55
+ yield cv2.imencode('.jpg', frame)[1].tobytes()
56
+ else:
57
+ break
58
+
59
+ gui = gr.Blocks()
60
+ with gui:
61
+ gr.Markdown("# Live Video AI Assistant")
62
+ with gr.Row():
63
+ video_component = gr.Video()
64
+ threading.Thread(target=video_feed, daemon=True).start()
65
+ prompt = gr.Textbox(label="Enter your question")
66
+ response = gr.Textbox(label="AI Response")
67
+ btn = gr.Button("Ask")
68
+ btn.click(process_image, inputs=prompt, outputs=response)
69
+
70
+ gui.launch()