Spaces:
Build error
Build error
Commit
•
46556a0
1
Parent(s):
283582b
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import cv2 #cv2==4.8.0
|
2 |
+
import numpy as np#numpy ==1.24.3
|
3 |
+
import streamlit as st#
|
4 |
+
import time
|
5 |
+
|
6 |
+
|
7 |
+
|
8 |
+
st.title("实时皮肤检测应用程序")
|
9 |
+
|
10 |
+
cap = cv2.VideoCapture(0)
|
11 |
+
|
12 |
+
if not cap.isOpened():
|
13 |
+
st.error("无法打开摄像头。")
|
14 |
+
else:
|
15 |
+
while True:
|
16 |
+
ret, frame = cap.read()
|
17 |
+
|
18 |
+
if not ret:
|
19 |
+
st.error("无法获取摄像头帧。")
|
20 |
+
break
|
21 |
+
|
22 |
+
ycbcr_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2YCrCb)
|
23 |
+
|
24 |
+
lower_skin = np.array([0, 133, 77], dtype="uint8")
|
25 |
+
upper_skin = np.array([255, 230, 255], dtype="uint8")
|
26 |
+
skin_mask = cv2.inRange(ycbcr_frame, lower_skin, upper_skin)
|
27 |
+
|
28 |
+
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (11, 11))
|
29 |
+
skin_mask = cv2.erode(skin_mask, kernel, iterations=2)
|
30 |
+
skin_mask = cv2.dilate(skin_mask, kernel, iterations=2)
|
31 |
+
|
32 |
+
skin_detected = cv2.bitwise_and(frame, frame, mask=skin_mask)
|
33 |
+
|
34 |
+
st.image(skin_detected, channels="BGR", caption="皮肤检测结果", use_column_width=True)
|
35 |
+
|
36 |
+
stop_button_id = str(time.time())
|
37 |
+
|
38 |
+
if st.button("停止按钮" + stop_button_id):
|
39 |
+
break
|
40 |
+
|
41 |
+
# 控制每秒只显示2张图像
|
42 |
+
time.sleep(1.0)
|
43 |
+
|
44 |
+
cap.release()
|
45 |
+
cv2.destroyAllWindows()
|