Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,34 +1,23 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
import cv2
|
| 3 |
-
|
| 4 |
-
from PIL import Image
|
| 5 |
|
| 6 |
-
|
| 7 |
-
def capture_camera():
|
| 8 |
-
cap = cv2.VideoCapture(0) # 参数0表示默认摄像头
|
| 9 |
-
if not cap.isOpened():
|
| 10 |
-
st.error("无法打开摄像头")
|
| 11 |
-
return None, None
|
| 12 |
-
ret, frame = cap.read()
|
| 13 |
-
cap.release()
|
| 14 |
-
if not ret:
|
| 15 |
-
st.error("无法从摄像头获取图像")
|
| 16 |
-
return None, None
|
| 17 |
-
return frame, cap
|
| 18 |
|
| 19 |
-
def main():
|
| 20 |
-
st.title("摄像头拍照应用")
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
if frame is not None:
|
| 26 |
-
# 转换颜色从BGR到RGB
|
| 27 |
-
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 28 |
-
# 将图像显示在Streamlit界面上
|
| 29 |
-
st.image(frame, use_column_width=True, caption="捕捉到的图像")
|
| 30 |
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
| 1 |
import cv2
|
| 2 |
+
from streamlit_webrtc import VideoTransformerBase, webrtc_streamer
|
|
|
|
| 3 |
|
| 4 |
+
faceCascade = cv2.CascadeClassifier(cv2.haarcascades+'haarcascade_frontalface_default.xml')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
class VideoTransformer(VideoTransformerBase):
|
| 8 |
+
def __init__(self):
|
| 9 |
+
self.i = 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
def transform(self, frame):
|
| 12 |
+
img = frame.to_ndarray(format="bgr24")
|
| 13 |
+
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
| 14 |
+
faces = faceCascade.detectMultiScale(gray, 1.3, 5)
|
| 15 |
+
i =self.i+1
|
| 16 |
+
for (x, y, w, h) in faces:
|
| 17 |
+
cv2.rectangle(img, (x, y), (x + w, y + h), (95, 207, 30), 3)
|
| 18 |
+
cv2.rectangle(img, (x, y - 40), (x + w, y), (95, 207, 30), -1)
|
| 19 |
+
cv2.putText(img, 'F-' + str(i), (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 0), 2)
|
| 20 |
|
| 21 |
+
return img
|
| 22 |
+
|
| 23 |
+
webrtc_streamer(key="example", video_transformer_factory=VideoTransformer)
|