Defer camera init and add debug logs
This commit is contained in:
31
main.py
31
main.py
@@ -26,8 +26,9 @@ load_dotenv()
|
|||||||
|
|
||||||
class ItemSenseApp(NSObject):
|
class ItemSenseApp(NSObject):
|
||||||
def applicationDidFinishLaunching_(self, notification):
|
def applicationDidFinishLaunching_(self, notification):
|
||||||
|
print("Application did finish launching...")
|
||||||
self.window = NSWindow.alloc().initWithContentRect_styleMask_backing_defer_(
|
self.window = NSWindow.alloc().initWithContentRect_styleMask_backing_defer_(
|
||||||
NSMakeRect(0, 0, 800, 700), # Increased height for text view
|
NSMakeRect(0, 0, 800, 700),
|
||||||
NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskResizable | NSWindowStyleMaskMiniaturizable,
|
NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskResizable | NSWindowStyleMaskMiniaturizable,
|
||||||
NSBackingStoreBuffered,
|
NSBackingStoreBuffered,
|
||||||
False
|
False
|
||||||
@@ -52,11 +53,7 @@ class ItemSenseApp(NSObject):
|
|||||||
self.scroll_view = NSScrollView.alloc().init()
|
self.scroll_view = NSScrollView.alloc().init()
|
||||||
self.scroll_view.setHasVerticalScroller_(True)
|
self.scroll_view.setHasVerticalScroller_(True)
|
||||||
self.scroll_view.setBorderType_(2) # NSBezelBorder
|
self.scroll_view.setBorderType_(2) # NSBezelBorder
|
||||||
|
self.scroll_view.setHeightAdjustLimit_(1.0)
|
||||||
# Determine size (approx) - needed for the content view mostly
|
|
||||||
# But StackView will resize it. Let's give it an intrinsic size or min size constraint if possible.
|
|
||||||
# For simplicity in PyObjC without autolayout constraints helpers:
|
|
||||||
self.scroll_view.setHeightAdjustLimit_(1.0) # ??? No, simple init is usually fine in stackview
|
|
||||||
|
|
||||||
# Text View
|
# Text View
|
||||||
content_size = self.scroll_view.contentSize()
|
content_size = self.scroll_view.contentSize()
|
||||||
@@ -72,36 +69,36 @@ class ItemSenseApp(NSObject):
|
|||||||
self.text_view.setRichText_(False)
|
self.text_view.setRichText_(False)
|
||||||
|
|
||||||
self.scroll_view.setDocumentView_(self.text_view)
|
self.scroll_view.setDocumentView_(self.text_view)
|
||||||
|
|
||||||
# We need to ensure the scroll view has some height. StackView might crush it if not careful.
|
|
||||||
# We can set a frame size, or add a constraint.
|
|
||||||
# Let's simple try adding it to stack view.
|
|
||||||
self.stack_view.addView_inGravity_(self.scroll_view, 2)
|
self.stack_view.addView_inGravity_(self.scroll_view, 2)
|
||||||
|
|
||||||
# Hide initially or just empty? Let's keep it visible so layout stabilizes.
|
self.text_view.setString_("Initializing camera...")
|
||||||
self.text_view.setString_("")
|
|
||||||
|
|
||||||
# Capture Button
|
# Capture Button
|
||||||
self.capture_button = NSButton.buttonWithTitle_target_action_("Capture", self, "captureClicked:")
|
self.capture_button = NSButton.buttonWithTitle_target_action_("Capture", self, "captureClicked:")
|
||||||
self.stack_view.addView_inGravity_(self.capture_button, 3) # Bottom gravity
|
self.stack_view.addView_inGravity_(self.capture_button, 3) # Bottom gravity
|
||||||
|
|
||||||
self.window.makeKeyAndOrderFront_(None)
|
self.window.makeKeyAndOrderFront_(None)
|
||||||
|
self.window.orderFrontRegardless() # Force it front
|
||||||
|
print("Window ordered front.")
|
||||||
|
|
||||||
# Initialize Camera
|
# Initialize Camera with a delay to allow UI to render first
|
||||||
|
self.performSelector_withObject_afterDelay_("initCamera:", None, 0.5)
|
||||||
|
|
||||||
|
def initCamera_(self, sender):
|
||||||
|
print("Initializing camera...")
|
||||||
self.cap = cv2.VideoCapture(0)
|
self.cap = cv2.VideoCapture(0)
|
||||||
if not self.cap.isOpened():
|
if not self.cap.isOpened():
|
||||||
NSLog("Error: Could not open camera")
|
NSLog("Error: Could not open camera")
|
||||||
self.text_view.setString_("Error: Could not open camera.")
|
self.text_view.setString_("Error: Could not open camera.")
|
||||||
|
return
|
||||||
# State
|
|
||||||
self.is_capturing = True
|
|
||||||
self.current_frame = None
|
|
||||||
|
|
||||||
|
print("Camera opened.")
|
||||||
# Start Timer for 30 FPS
|
# Start Timer for 30 FPS
|
||||||
self.timer = NSTimer.scheduledTimerWithTimeInterval_target_selector_userInfo_repeats_(
|
self.timer = NSTimer.scheduledTimerWithTimeInterval_target_selector_userInfo_repeats_(
|
||||||
1.0/30.0, self, "updateFrame:", None, True
|
1.0/30.0, self, "updateFrame:", None, True
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def applicationShouldTerminateAfterLastWindowClosed_(self, sender):
|
def applicationShouldTerminateAfterLastWindowClosed_(self, sender):
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user