Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

제발 도와주세요 #703

Open
akrwldmsdbwjspdla opened this issue Aug 28, 2024 · 1 comment
Open

제발 도와주세요 #703

akrwldmsdbwjspdla opened this issue Aug 28, 2024 · 1 comment
Labels
question 질문으로 사용될 이슈

Comments

@akrwldmsdbwjspdla
Copy link

요약





상황

이 어플을 빨리 만들어서 제출해야 하는데 계속 작동하다가 멈춥니다. update함수에 while loop 사용해서 실행했을 때는 멈추지 않았지만 다른 함수 호출이 불가능했습니다. 혹시 어플이 안멈추게 하거나 while loop 실행 중 버튼이 작동하게 만들어 주시거나 방법을 알려주실 능력자분 계시나요. ㅠㅠ





`import cv2
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.image import Image as KivyImage
from kivy.clock import Clock
from kivy.graphics.texture import Texture
import pyttsx3
from keras.models import load_model
from PIL import Image, ImageOps
import numpy as np
import tensorflow
from tensorflow.keras.layers import DepthwiseConv2D

class RecorderApp(App):
def build(self):
self.layout = BoxLayout(orientation='vertical')
self.record_button = Button(text='Start Webcam')
self.record_button.bind(on_press=self.toggle_webcam)
self.layout.add_widget(self.record_button)
self.image = KivyImage()
self.layout.add_widget(self.image)
self.capture = None
self.is_camera_on = False
self.engine = pyttsx3.init()
self.class_names = open("C:/Users/User/Downloads/converted_keras/labels.txt", "r", encoding="utf-8").readlines()
self.model = load_model("C:/Users/User/Downloads/converted_keras/keras_model.h5", custom_objects={'DepthwiseConv2D': DepthwiseConv2D}, compile=False)
self.last_class_name = None # 마지막 예측 결과를 저장할 변수 초기화
return self.layout

def toggle_webcam(self, instance):
    if not self.is_camera_on:
        # 웹캠 시작
        self.record_button.text = 'Stop Webcam'
        self.is_camera_on = True

        # OpenCV 시작 (DirectShow 백엔드 사용)
        self.capture = cv2.VideoCapture(0, cv2.CAP_DSHOW)

        if not self.capture.isOpened():
            print("웹캠을 열 수 없습니다.")
            self.engine.say("웹캠을 열 수 없습니다.")
            self.engine.runAndWait()
            return

        # 해상도 설정
        self.capture.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
        self.capture.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)

        # 10fps
        Clock.schedule_interval(self.update, 1.0 / 10.0)
    else:
        # 웹캠 중지
        self.record_button.text = 'Start Webcam'
        self.is_camera_on = False

        # 종료
        if self.capture is not None:
            self.capture.release()

        Clock.unschedule(self.update)

def update(self, dt):
    ret, frame = self.capture.read()
    if ret:
        # 상하좌우 반전
        frame = cv2.flip(frame, -1)  # -1은 상하좌우 모두 반전

        # BGR to RGB 변환
        frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        texture = Texture.create(size=(frame.shape[1], frame.shape[0]), colorfmt='rgb')
        texture.blit_buffer(frame_rgb.tobytes(), bufferfmt='ubyte', colorfmt='rgb')
        self.image.texture = texture

        # Keras 모델을 사용한 예측
        image_pil = Image.fromarray(frame_rgb)
        size = (224, 224)
        image_pil = ImageOps.fit(image_pil, size, Image.Resampling.LANCZOS)
        image_array = np.asarray(image_pil)
        normalized_image_array = (image_array.astype(np.float32) / 127.5) - 1
        data = np.ndarray(shape=(1, 224, 224, 3), dtype=np.float32)
        data[0] = normalized_image_array

        prediction = self.model.predict(data)
        index = np.argmax(prediction)
        class_name = self.class_names[index].strip()
        confidence_score = prediction[0][index]

        # 예측 결과 출력 및 TTS로 읽기 (새로운 클래스일 때만)
        if class_name != self.last_class_name:
            print(class_name)
            self.engine.say(class_name)
            self.engine.runAndWait()
            self.last_class_name = class_name

def on_stop(self):
    # 앱 종료 시 리소스 정리
    if self.capture is not None:
        self.capture.release()
    self.engine.stop()

if name == 'main':
RecorderApp().run()
converted_keras.zip
`

@akrwldmsdbwjspdla akrwldmsdbwjspdla added the question 질문으로 사용될 이슈 label Aug 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question 질문으로 사용될 이슈
Projects
None yet
Development

No branches or pull requests

2 participants
@akrwldmsdbwjspdla and others