Untitled

 avatar
unknown
plain_text
4 months ago
4.3 kB
2
Indexable
def on_press(self, event):
    current_time = time.time()
    if current_time - self.last_click_time >= self.click_cooldown:
        self.is_pressed = True
        self.press_animation_active = True
        self.last_click_time = current_time
        
        if not self.classifier:
            print(TRANSLATIONS[self.winfo_toplevel().LANGUAGE]['camera_error'])
            return
            
        try:
            print("Scan button pressed - starting classification")
            
            # Create loading screen
            loading_screen = LoadingScreen(
                self.winfo_toplevel(),
                message=TRANSLATIONS[self.winfo_toplevel().LANGUAGE]['scanning_object'],
                dark_mode=self.winfo_toplevel().DARK_MODE,
                language=self.winfo_toplevel().LANGUAGE
            )
            
            def perform_classification():
                try:
                    # Take picture and classify in a separate thread
                    def classification_thread():
                        try:
                            # First take picture
                            image_path = self.classifier.take_picture()
                            if not image_path or not os.path.exists(image_path):
                                raise Exception("Failed to capture image")
                                
                            # Then classify
                            result = self.classifier.classify_image(image_path)
                            if not result:
                                raise Exception("No classification result")
                            
                            # Use after() to safely update UI from the main thread
                            self.after(0, lambda: handle_classification_complete(result))
                        except Exception as e:
                            print(f"Error during classification: {str(e)}")
                            self.after(0, lambda: handle_classification_error(str(e)))
                    
                    def handle_classification_complete(result):
                        try:
                            loading_screen.destroy()
                            ClassificationResultScreen(
                                self.winfo_toplevel(),
                                result,
                                self.winfo_toplevel().DARK_MODE,
                                self.winfo_toplevel().LANGUAGE
                            )
                        except Exception as e:
                            print(f"Error showing result: {str(e)}")
                            loading_screen.destroy()
                            
                    def handle_classification_error(error_message):
                        print(f"Classification error: {error_message}")
                        loading_screen.destroy()
                        MessageDialog(
                            self.winfo_toplevel(),
                            TRANSLATIONS[self.winfo_toplevel().LANGUAGE]['classification_error'],
                            self.winfo_toplevel().DARK_MODE
                        )
                    
                    # Start classification in separate thread
                    thread = threading.Thread(target=classification_thread)
                    thread.daemon = True
                    thread.start()
                    
                except Exception as e:
                    print(f"Error setting up classification: {str(e)}")
                    loading_screen.destroy()
                    MessageDialog(
                        self.winfo_toplevel(),
                        TRANSLATIONS[self.winfo_toplevel().LANGUAGE]['classification_error'],
                        self.winfo_toplevel().DARK_MODE
                    )
            
            # Schedule the classification to run after the animation starts
            self.after(100, perform_classification)
            
        except Exception as e:
            print(f"Error setting up classification: {str(e)}")
            MessageDialog(
                self.winfo_toplevel(),
                TRANSLATIONS[self.winfo_toplevel().LANGUAGE]['classification_error'],
                self.winfo_toplevel().DARK_MODE
            )
    else:
        print("Scan button cooldown active")
Editor is loading...
Leave a Comment