Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,81 +1 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import sys
|
| 3 |
-
from PyQt5.QtWidgets import QApplication, QLabel, QPushButton, QVBoxLayout, QHBoxLayout, QWidget, QGridLayout, QFileDialog
|
| 4 |
-
from PyQt5.QtGui import QPixmap, QImage
|
| 5 |
-
from PyQt5.QtCore import QTimer
|
| 6 |
-
import cv2
|
| 7 |
-
|
| 8 |
-
class MainWindow(QWidget):
|
| 9 |
-
def __init__(self):
|
| 10 |
-
super().__init__()
|
| 11 |
-
|
| 12 |
-
# Tạo layout chính
|
| 13 |
-
main_layout = QHBoxLayout()
|
| 14 |
-
|
| 15 |
-
# Layout bên trái - Hiển thị video
|
| 16 |
-
self.video_label = QLabel("Video Stream")
|
| 17 |
-
self.video_label.setFixedSize(640, 480)
|
| 18 |
-
|
| 19 |
-
left_layout = QVBoxLayout()
|
| 20 |
-
left_layout.addWidget(self.video_label)
|
| 21 |
-
|
| 22 |
-
# Layout bên phải - Nhập ảnh và hiển thị 3 ảnh xuất ra
|
| 23 |
-
right_layout = QVBoxLayout()
|
| 24 |
-
|
| 25 |
-
# Button để nhập ảnh
|
| 26 |
-
self.btn_upload = QPushButton("Chọn ảnh để xử lý")
|
| 27 |
-
self.btn_upload.clicked.connect(self.upload_image)
|
| 28 |
-
|
| 29 |
-
# Labels để hiển thị 3 ảnh xuất ra
|
| 30 |
-
self.result_label_1 = QLabel("Ảnh kết quả 1")
|
| 31 |
-
self.result_label_2 = QLabel("Ảnh kết quả 2")
|
| 32 |
-
self.result_label_3 = QLabel("Ảnh kết quả 3")
|
| 33 |
-
|
| 34 |
-
right_layout.addWidget(self.btn_upload)
|
| 35 |
-
right_layout.addWidget(self.result_label_1)
|
| 36 |
-
right_layout.addWidget(self.result_label_2)
|
| 37 |
-
right_layout.addWidget(self.result_label_3)
|
| 38 |
-
|
| 39 |
-
# Thêm layout trái và phải vào layout chính
|
| 40 |
-
main_layout.addLayout(left_layout)
|
| 41 |
-
main_layout.addLayout(right_layout)
|
| 42 |
-
|
| 43 |
-
# Đặt layout chính cho cửa sổ
|
| 44 |
-
self.setLayout(main_layout)
|
| 45 |
-
|
| 46 |
-
# Khởi động camera để stream video
|
| 47 |
-
self.cap = cv2.VideoCapture(0)
|
| 48 |
-
self.timer = QTimer()
|
| 49 |
-
self.timer.timeout.connect(self.update_frame)
|
| 50 |
-
self.timer.start(30) # Cập nhật mỗi 30ms
|
| 51 |
-
|
| 52 |
-
def upload_image(self):
|
| 53 |
-
# Chọn ảnh từ máy tính
|
| 54 |
-
options = QFileDialog.Options()
|
| 55 |
-
file_name, _ = QFileDialog.getOpenFileName(self, "Chọn ảnh", "", "Image Files (*.png *.jpg *.jpeg *.bmp)", options=options)
|
| 56 |
-
|
| 57 |
-
if file_name:
|
| 58 |
-
# Load ảnh và hiển thị trên label (ví dụ ảnh kết quả thứ nhất)
|
| 59 |
-
pixmap = QPixmap(file_name)
|
| 60 |
-
self.result_label_1.setPixmap(pixmap.scaled(200, 200))
|
| 61 |
-
|
| 62 |
-
def update_frame(self):
|
| 63 |
-
# Cập nhật frame từ camera
|
| 64 |
-
ret, frame = self.cap.read()
|
| 65 |
-
if ret:
|
| 66 |
-
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 67 |
-
height, width, channel = frame.shape
|
| 68 |
-
step = channel * width
|
| 69 |
-
q_img = QImage(frame.data, width, height, step, QImage.Format_RGB888)
|
| 70 |
-
self.video_label.setPixmap(QPixmap.fromImage(q_img))
|
| 71 |
-
|
| 72 |
-
def closeEvent(self, event):
|
| 73 |
-
# Giải phóng camera khi đóng cửa sổ
|
| 74 |
-
self.cap.release()
|
| 75 |
-
|
| 76 |
-
if __name__ == "__main__":
|
| 77 |
-
app = QApplication(sys.argv)
|
| 78 |
-
window = MainWindow()
|
| 79 |
-
window.setWindowTitle("Giao diện xử lý ảnh và video")
|
| 80 |
-
window.show()
|
| 81 |
-
sys.exit(app.exec_())
|
|
|
|
| 1 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|