File size: 12,967 Bytes
3cbf6c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
import streamlit as st
import pandas as pd
from datetime import datetime
import cv2
import pyaudio
import wave
import imageio
import av
import moviepy.editor as mp
import os
import numpy as np
from io import BytesIO

# Initialize session state
if 'file_history' not in st.session_state:
    st.session_state['file_history'] = []
if 'ping_code' not in st.session_state:
    st.session_state['ping_code'] = ""
if 'uploaded_files' not in st.session_state:
    st.session_state['uploaded_files'] = []

# Function to save to history
def save_to_history(file_type, file_path):
    timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    st.session_state['file_history'].append({
        "Timestamp": timestamp,
        "Type": file_type,
        "Path": file_path
    })

# Sidebar configuration
st.sidebar.header("πŸ“Έ Configuration")
library_choice = st.sidebar.selectbox(
    "Select Library",
    ["OpenCV", "PyAudio", "ImageIO", "PyAV", "MoviePy"]
)
resolution = st.sidebar.select_slider(
    "Resolution",
    options=["320x240", "640x480", "1280x720"],
    value="640x480"
)
fps = st.sidebar.slider("FPS", 1, 60, 30)

# DTMF-style ping code input
st.sidebar.subheader("πŸ”’ Device Ping Code")
col1, col2, col3, col4 = st.sidebar.columns(4)
with col1:
    digit1 = st.selectbox("D1", [str(i) for i in range(10)], key="d1")
with col2:
    digit2 = st.selectbox("D2", [str(i) for i in range(10)], key="d2")
with col3:
    digit3 = st.selectbox("D3", [str(i) for i in range(10)], key="d3")
with col4:
    digit4 = st.selectbox("D4", [str(i) for i in range(10)], key="d4")
ping_code = digit1 + digit2 + digit3 + digit4
st.session_state['ping_code'] = ping_code
st.sidebar.write(f"Ping Code: {ping_code}")

# Main UI
st.title("πŸ“Έ Camera & Audio Capture Tool")

# Outline with top 3 function examples
st.header("πŸ“Έ Top Five Python Libraries")

# 1. OpenCV
with st.expander("1. πŸ“· OpenCV"):
    st.write("πŸŽ₯ *Best Feature*: Real-time image processing and video capture.")
    st.subheader("Top 3 Function Examples")
    
    st.write("1. `cv2.VideoCapture()` - Capture and display video")
    if st.button("Run VideoCapture", key="opencv_1"):
        cap = cv2.VideoCapture(0)
        frame_placeholder = st.empty()
        for _ in range(50):  # 50 frames
            ret, frame = cap.read()
            if ret:
                frame_placeholder.image(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
        cap.release()

    st.write("2. `cv2.imwrite()` - Save processed image")
    if st.button("Run imwrite", key="opencv_2"):
        cap = cv2.VideoCapture(0)
        ret, frame = cap.read()
        if ret:
            gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)  # Convert to grayscale
            file_path = f"opencv_gray_{datetime.now().strftime('%Y%m%d_%H%M%S')}.jpg"
            cv2.imwrite(file_path, gray)
            save_to_history("Image", file_path)
            st.image(file_path, caption="Grayscale Image")
        cap.release()

    st.write("3. `cv2.Canny()` - Edge detection")
    if st.button("Run Canny", key="opencv_3"):
        cap = cv2.VideoCapture(0)
        ret, frame = cap.read()
        if ret:
            edges = cv2.Canny(frame, 100, 200)
            file_path = f"opencv_edges_{datetime.now().strftime('%Y%m%d_%H%M%S')}.jpg"
            cv2.imwrite(file_path, edges)
            save_to_history("Image", file_path)
            st.image(file_path, caption="Edge Detection")
        cap.release()

# 2. PyAudio
with st.expander("2. πŸŽ™οΈ PyAudio"):
    st.write("πŸ”Š *Best Feature*: Low-level audio input/output control.")
    st.subheader("Top 3 Function Examples")

    st.write("1. `PyAudio.open()` - Record audio")
    if st.button("Run Audio Record", key="pyaudio_1"):
        p = pyaudio.PyAudio()
        stream = p.open(format=pyaudio.paInt16, channels=1, rate=44100, input=True, frames_per_buffer=1024)
        frames = [stream.read(1024) for _ in range(int(44100 / 1024 * 3))]  # 3 seconds
        stream.stop_stream()
        stream.close()
        p.terminate()
        file_path = f"pyaudio_rec_{datetime.now().strftime('%Y%m%d_%H%M%S')}.wav"
        with wave.open(file_path, 'wb') as wf:
            wf.setnchannels(1)
            wf.setsampwidth(p.get_sample_size(pyaudio.paInt16))
            wf.setframerate(44100)
            wf.writeframes(b''.join(frames))
        save_to_history("Audio", file_path)
        st.audio(file_path)

    st.write("2. `PyAudio.get_device_info_by_index()` - List audio devices")
    if st.button("Run Device Info", key="pyaudio_2"):
        p = pyaudio.PyAudio()
        devices = [p.get_device_info_by_index(i) for i in range(p.get_device_count())]
        st.write("Available Audio Devices:", devices)
        p.terminate()

    st.write("3. `stream.read()` - Real-time audio data")
    if st.button("Run Stream Read", key="pyaudio_3"):
        p = pyaudio.PyAudio()
        stream = p.open(format=pyaudio.paInt16, channels=1, rate=44100, input=True, frames_per_buffer=1024)
        data = stream.read(1024)
        st.write("Sample audio data length:", len(data))
        stream.stop_stream()
        stream.close()
        p.terminate()

# 3. ImageIO
with st.expander("3. πŸ“Ή ImageIO"):
    st.write("πŸŽ₯ *Best Feature*: Simple and efficient image/video I/O.")
    st.subheader("Top 3 Function Examples")

    st.write("1. `imageio.get_reader()` - Read webcam frames")
    if st.button("Run get_reader", key="imageio_1"):
        reader = imageio.get_reader('<video0>')
        frame = reader.get_next_data()
        file_path = f"imageio_frame_{datetime.now().strftime('%Y%m%d_%H%M%S')}.jpg"
        imageio.imwrite(file_path, frame)
        save_to_history("Image", file_path)
        st.image(file_path)

    st.write("2. `imageio.imwrite()` - Save image with compression")
    if st.button("Run imwrite", key="imageio_2"):
        reader = imageio.get_reader('<video0>')
        frame = reader.get_next_data()
        file_path = f"imageio_comp_{datetime.now().strftime('%Y%m%d_%H%M%S')}.jpg"
        imageio.imwrite(file_path, frame, quality=85)  # Compressed
        save_to_history("Image", file_path)
        st.image(file_path, caption="Compressed Image")

    st.write("3. `imageio.mimwrite()` - Create GIF")
    if st.button("Run mimwrite", key="imageio_3"):
        reader = imageio.get_reader('<video0>')
        frames = [reader.get_next_data() for _ in range(10)]
        file_path = f"imageio_gif_{datetime.now().strftime('%Y%m%d_%H%M%S')}.gif"
        imageio.mimwrite(file_path, frames, fps=5)
        save_to_history("GIF", file_path)
        st.image(file_path, caption="GIF")

# 4. PyAV
with st.expander("4. 🎬 PyAV"):
    st.write("πŸŽ₯ *Best Feature*: Powerful FFmpeg-based AV processing.")
    st.subheader("Top 3 Function Examples")

    st.write("1. `av.open()` - Capture video")
    if st.button("Run av.open", key="pyav_1"):
        container = av.open('/dev/video0')
        stream = container.streams.video[0]
        file_path = f"pyav_vid_{datetime.now().strftime('%Y%m%d_%H%M%S')}.mp4"
        output = av.open(file_path, 'w')
        out_stream = output.add_stream('h264', rate=30)
        for i, frame in enumerate(container.decode(stream)):
            if i > 30: break
            out_frame = frame.reformat(out_stream.width, out_stream.height)
            output.mux(out_stream.encode(out_frame))
        output.close()
        container.close()
        save_to_history("Video", file_path)
        st.video(file_path)

    st.write("2. `container.decode()` - Extract audio")
    if st.button("Run decode", key="pyav_2"):
        container = av.open('/dev/video0', 'r', format='v4l2')
        file_path = f"pyav_audio_{datetime.now().strftime('%Y%m%d_%H%M%S')}.wav"
        output = av.open(file_path, 'w')
        out_stream = output.add_stream('pcm_s16le', rate=44100)
        for packet in container.demux():
            for frame in packet.decode():
                if frame.is_corrupt: continue
                if hasattr(frame, 'to_ndarray'):
                    output.mux(out_stream.encode(frame))
            if os.path.getsize(file_path) > 100000: break  # Limit size
        output.close()
        container.close()
        save_to_history("Audio", file_path)
        st.audio(file_path)

    st.write("3. `av.filter()` - Apply video filter")
    if st.button("Run filter", key="pyav_3"):
        container = av.open('/dev/video0')
        stream = container.streams.video[0]
        file_path = f"pyav_filter_{datetime.now().strftime('%Y%m%d_%H%M%S')}.mp4"
        output = av.open(file_path, 'w')
        out_stream = output.add_stream('h264', rate=30)
        graph = av.filter.Graph()
        filt = graph.add("negate")  # Invert colors
        for i, frame in enumerate(container.decode(stream)):
            if i > 30: break
            filt.push(frame)
            out_frame = filt.pull()
            output.mux(out_stream.encode(out_frame.reformat(out_stream.width, out_stream.height)))
        output.close()
        container.close()
        save_to_history("Video", file_path)
        st.video(file_path, caption="Negated Video")

# 5. MoviePy
with st.expander("5. πŸ“Ό MoviePy"):
    st.write("πŸŽ₯ *Best Feature*: High-level video editing.")
    st.subheader("Top 3 Function Examples")

    st.write("1. `ImageSequenceClip()` - Video from frames")
    if st.button("Run ImageSequenceClip", key="moviepy_1"):
        cap = cv2.VideoCapture(0)
        frames = [cv2.cvtColor(cap.read()[1], cv2.COLOR_BGR2RGB) for _ in range(30)]
        cap.release()
        file_path = f"moviepy_seq_{datetime.now().strftime('%Y%m%d_%H%M%S')}.mp4"
        clip = mp.ImageSequenceClip(frames, fps=15)
        clip.write_videofile(file_path)
        save_to_history("Video", file_path)
        st.video(file_path)

    st.write("2. `VideoFileClip()` - Load and resize video")
    if st.button("Run VideoFileClip", key="moviepy_2"):
        cap = cv2.VideoCapture(0)
        frames = [cv2.cvtColor(cap.read()[1], cv2.COLOR_BGR2RGB) for _ in range(30)]
        cap.release()
        temp_path = "temp.mp4"
        mp.ImageSequenceClip(frames, fps=15).write_videofile(temp_path)
        clip = mp.VideoFileClip(temp_path).resize((320, 240))
        file_path = f"moviepy_resized_{datetime.now().strftime('%Y%m%d_%H%M%S')}.mp4"
        clip.write_videofile(file_path)
        save_to_history("Video", file_path)
        st.video(file_path, caption="Resized Video")

    st.write("3. `concatenate_videoclips()` - Combine clips")
    if st.button("Run concatenate", key="moviepy_3"):
        cap = cv2.VideoCapture(0)
        frames1 = [cv2.cvtColor(cap.read()[1], cv2.COLOR_BGR2RGB) for _ in range(15)]
        frames2 = [cv2.cvtColor(cap.read()[1], cv2.COLOR_BGR2RGB) for _ in range(15)]
        cap.release()
        clip1 = mp.ImageSequenceClip(frames1, fps=15)
        clip2 = mp.ImageSequenceClip(frames2, fps=15)
        final_clip = mp.concatenate_videoclips([clip1, clip2])
        file_path = f"moviepy_concat_{datetime.now().strftime('%Y%m%d_%H%M%S')}.mp4"
        final_clip.write_videofile(file_path)
        save_to_history("Video", file_path)
        st.video(file_path, caption="Concatenated Video")

# Media Upload Section
st.header("πŸ“‚ Upload Media Files")
uploaded_files = st.file_uploader("Upload Images, Audio, or Video", accept_multiple_files=True, type=['jpg', 'png', 'mp4', 'wav', 'mp3'])
if uploaded_files:
    for uploaded_file in uploaded_files:
        file_type = uploaded_file.type.split('/')[0]
        file_path = f"uploaded_{uploaded_file.name}"
        with open(file_path, 'wb') as f:
            f.write(uploaded_file.read())
        st.session_state['uploaded_files'].append({
            "Name": uploaded_file.name,
            "Type": file_type,
            "Path": file_path
        })

# Galleries and Players
st.header("πŸ–ΌοΈ Media Gallery")
if st.session_state['uploaded_files']:
    images = [f for f in st.session_state['uploaded_files'] if f['Type'] == 'image']
    audios = [f for f in st.session_state['uploaded_files'] if f['Type'] == 'audio']
    videos = [f for f in st.session_state['uploaded_files'] if f['Type'] == 'video']

    if images:
        st.subheader("Images")
        cols = st.columns(3)
        for i, img in enumerate(images):
            with cols[i % 3]:
                st.image(img['Path'], caption=img['Name'], use_column_width=True)

    if audios:
        st.subheader("Audio")
        for audio in audios:
            st.audio(audio['Path'], format=f"audio/{audio['Name'].split('.')[-1]}")
            st.write(f"Name: {audio['Name']}")

    if videos:
        st.subheader("Videos")
        for video in videos:
            st.video(video['Path'])
            st.write(f"Name: {video['Name']}")
else:
    st.write("No media uploaded yet.")

# File History Display
st.header("πŸ“œ File History")
if st.session_state['file_history']:
    df = pd.DataFrame(st.session_state['file_history'])
    st.dataframe(df)
else:
    st.write("No files captured yet.")