diff --git a/.gitignore b/.gitignore index 48643ec..e8277db 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,21 @@ __pycache__/ *.pyc .DS_Store .specify/ + +# iOS / Xcode +build/ +DerivedData/ +*.moved-aside +*.rxproj +*.tps +*.xcodeproj/project.xcworkspace +*.xcodeproj/xcuserdata +*.xcworkspace/xcuserdata +*.xccheckout +*.xcscmblueprint + +# CocoaPods +Pods/ + + +iOS/ItemSense/build/ diff --git a/iOS/ItemSense/ItemSense.xcodeproj/project.pbxproj b/iOS/ItemSense/ItemSense.xcodeproj/project.pbxproj new file mode 100644 index 0000000..64693e4 --- /dev/null +++ b/iOS/ItemSense/ItemSense.xcodeproj/project.pbxproj @@ -0,0 +1,339 @@ +// !$*UTF8*$! +{ + archiveVersion = 1; + classes = { + }; + objectVersion = 77; + objects = { + +/* Begin PBXFileReference section */ + 7F1445BE2F216C600065C89B /* ItemSense.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = ItemSense.app; sourceTree = BUILT_PRODUCTS_DIR; }; +/* End PBXFileReference section */ + +/* Begin PBXFileSystemSynchronizedRootGroup section */ + 7F1445C02F216C600065C89B /* ItemSense */ = { + isa = PBXFileSystemSynchronizedRootGroup; + path = ItemSense; + sourceTree = ""; + }; +/* End PBXFileSystemSynchronizedRootGroup section */ + +/* Begin PBXFrameworksBuildPhase section */ + 7F1445BB2F216C600065C89B /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXFrameworksBuildPhase section */ + +/* Begin PBXGroup section */ + 7F1445B52F216C600065C89B = { + isa = PBXGroup; + children = ( + 7F1445C02F216C600065C89B /* ItemSense */, + 7F1445BF2F216C600065C89B /* Products */, + ); + sourceTree = ""; + }; + 7F1445BF2F216C600065C89B /* Products */ = { + isa = PBXGroup; + children = ( + 7F1445BE2F216C600065C89B /* ItemSense.app */, + ); + name = Products; + sourceTree = ""; + }; +/* End PBXGroup section */ + +/* Begin PBXNativeTarget section */ + 7F1445BD2F216C600065C89B /* ItemSense */ = { + isa = PBXNativeTarget; + buildConfigurationList = 7F1445C92F216C610065C89B /* Build configuration list for PBXNativeTarget "ItemSense" */; + buildPhases = ( + 7F1445BA2F216C600065C89B /* Sources */, + 7F1445BB2F216C600065C89B /* Frameworks */, + 7F1445BC2F216C600065C89B /* Resources */, + ); + buildRules = ( + ); + dependencies = ( + ); + fileSystemSynchronizedGroups = ( + 7F1445C02F216C600065C89B /* ItemSense */, + ); + name = ItemSense; + packageProductDependencies = ( + ); + productName = ItemSense; + productReference = 7F1445BE2F216C600065C89B /* ItemSense.app */; + productType = "com.apple.product-type.application"; + }; +/* End PBXNativeTarget section */ + +/* Begin PBXProject section */ + 7F1445B62F216C600065C89B /* Project object */ = { + isa = PBXProject; + attributes = { + BuildIndependentTargetsInParallel = 1; + LastSwiftUpdateCheck = 2620; + LastUpgradeCheck = 2620; + TargetAttributes = { + 7F1445BD2F216C600065C89B = { + CreatedOnToolsVersion = 26.2; + }; + }; + }; + buildConfigurationList = 7F1445B92F216C600065C89B /* Build configuration list for PBXProject "ItemSense" */; + developmentRegion = en; + hasScannedForEncodings = 0; + knownRegions = ( + en, + Base, + ); + mainGroup = 7F1445B52F216C600065C89B; + minimizedProjectReferenceProxies = 1; + preferredProjectObjectVersion = 77; + productRefGroup = 7F1445BF2F216C600065C89B /* Products */; + projectDirPath = ""; + projectRoot = ""; + targets = ( + 7F1445BD2F216C600065C89B /* ItemSense */, + ); + }; +/* End PBXProject section */ + +/* Begin PBXResourcesBuildPhase section */ + 7F1445BC2F216C600065C89B /* Resources */ = { + isa = PBXResourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXResourcesBuildPhase section */ + +/* Begin PBXSourcesBuildPhase section */ + 7F1445BA2F216C600065C89B /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXSourcesBuildPhase section */ + +/* Begin XCBuildConfiguration section */ + 7F1445C72F216C610065C89B /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + ASSETCATALOG_COMPILER_GENERATE_SWIFT_ASSET_SYMBOL_EXTENSIONS = YES; + CLANG_ANALYZER_NONNULL = YES; + CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++20"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_ENABLE_OBJC_WEAK = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = dwarf; + DEVELOPMENT_TEAM = 7X85543FQQ; + ENABLE_STRICT_OBJC_MSGSEND = YES; + ENABLE_TESTABILITY = YES; + ENABLE_USER_SCRIPT_SANDBOXING = YES; + GCC_C_LANGUAGE_STANDARD = gnu17; + GCC_DYNAMIC_NO_PIC = NO; + GCC_NO_COMMON_BLOCKS = YES; + GCC_OPTIMIZATION_LEVEL = 0; + GCC_PREPROCESSOR_DEFINITIONS = ( + "DEBUG=1", + "$(inherited)", + ); + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + IPHONEOS_DEPLOYMENT_TARGET = 26.2; + LOCALIZATION_PREFERS_STRING_CATALOGS = YES; + MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; + MTL_FAST_MATH = YES; + ONLY_ACTIVE_ARCH = YES; + SDKROOT = iphoneos; + SWIFT_ACTIVE_COMPILATION_CONDITIONS = "DEBUG $(inherited)"; + SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + }; + name = Debug; + }; + 7F1445C82F216C610065C89B /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + ASSETCATALOG_COMPILER_GENERATE_SWIFT_ASSET_SYMBOL_EXTENSIONS = YES; + CLANG_ANALYZER_NONNULL = YES; + CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++20"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_ENABLE_OBJC_WEAK = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; + DEVELOPMENT_TEAM = 7X85543FQQ; + ENABLE_NS_ASSERTIONS = NO; + ENABLE_STRICT_OBJC_MSGSEND = YES; + ENABLE_USER_SCRIPT_SANDBOXING = YES; + GCC_C_LANGUAGE_STANDARD = gnu17; + GCC_NO_COMMON_BLOCKS = YES; + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + IPHONEOS_DEPLOYMENT_TARGET = 26.2; + LOCALIZATION_PREFERS_STRING_CATALOGS = YES; + MTL_ENABLE_DEBUG_INFO = NO; + MTL_FAST_MATH = YES; + SDKROOT = iphoneos; + SWIFT_COMPILATION_MODE = wholemodule; + VALIDATE_PRODUCT = YES; + }; + name = Release; + }; + 7F1445CA2F216C610065C89B /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; + CODE_SIGN_STYLE = Automatic; + CURRENT_PROJECT_VERSION = 1; + DEVELOPMENT_TEAM = 7X85543FQQ; + ENABLE_PREVIEWS = YES; + GENERATE_INFOPLIST_FILE = YES; + INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES; + INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES; + INFOPLIST_KEY_UILaunchScreen_Generation = YES; + INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; + INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; + INFOPLIST_KEY_NSCameraUsageDescription = "ItemSense needs camera access to identify items."; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/Frameworks", + ); + MARKETING_VERSION = 1.0; + PRODUCT_BUNDLE_IDENTIFIER = com.jaredlog.ItemSense; + PRODUCT_NAME = "$(TARGET_NAME)"; + STRING_CATALOG_GENERATE_SYMBOLS = YES; + SWIFT_APPROACHABLE_CONCURRENCY = YES; + SWIFT_DEFAULT_ACTOR_ISOLATION = MainActor; + SWIFT_EMIT_LOC_STRINGS = YES; + SWIFT_UPCOMING_FEATURE_MEMBER_IMPORT_VISIBILITY = YES; + SWIFT_VERSION = 5.0; + TARGETED_DEVICE_FAMILY = "1,2"; + }; + name = Debug; + }; + 7F1445CB2F216C610065C89B /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; + CODE_SIGN_STYLE = Automatic; + CURRENT_PROJECT_VERSION = 1; + DEVELOPMENT_TEAM = 7X85543FQQ; + ENABLE_PREVIEWS = YES; + GENERATE_INFOPLIST_FILE = YES; + INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES; + INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES; + INFOPLIST_KEY_UILaunchScreen_Generation = YES; + INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; + INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; + INFOPLIST_KEY_NSCameraUsageDescription = "ItemSense needs camera access to identify items."; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/Frameworks", + ); + MARKETING_VERSION = 1.0; + PRODUCT_BUNDLE_IDENTIFIER = com.jaredlog.ItemSense; + PRODUCT_NAME = "$(TARGET_NAME)"; + STRING_CATALOG_GENERATE_SYMBOLS = YES; + SWIFT_APPROACHABLE_CONCURRENCY = YES; + SWIFT_DEFAULT_ACTOR_ISOLATION = MainActor; + SWIFT_EMIT_LOC_STRINGS = YES; + SWIFT_UPCOMING_FEATURE_MEMBER_IMPORT_VISIBILITY = YES; + SWIFT_VERSION = 5.0; + TARGETED_DEVICE_FAMILY = "1,2"; + }; + name = Release; + }; +/* End XCBuildConfiguration section */ + +/* Begin XCConfigurationList section */ + 7F1445B92F216C600065C89B /* Build configuration list for PBXProject "ItemSense" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 7F1445C72F216C610065C89B /* Debug */, + 7F1445C82F216C610065C89B /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + 7F1445C92F216C610065C89B /* Build configuration list for PBXNativeTarget "ItemSense" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 7F1445CA2F216C610065C89B /* Debug */, + 7F1445CB2F216C610065C89B /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; +/* End XCConfigurationList section */ + }; + rootObject = 7F1445B62F216C600065C89B /* Project object */; +} diff --git a/iOS/ItemSense/ItemSense.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/iOS/ItemSense/ItemSense.xcodeproj/project.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..919434a --- /dev/null +++ b/iOS/ItemSense/ItemSense.xcodeproj/project.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/iOS/ItemSense/ItemSense.xcodeproj/project.xcworkspace/xcuserdata/jared.xcuserdatad/UserInterfaceState.xcuserstate b/iOS/ItemSense/ItemSense.xcodeproj/project.xcworkspace/xcuserdata/jared.xcuserdatad/UserInterfaceState.xcuserstate new file mode 100644 index 0000000..754c44e Binary files /dev/null and b/iOS/ItemSense/ItemSense.xcodeproj/project.xcworkspace/xcuserdata/jared.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/iOS/ItemSense/ItemSense.xcodeproj/xcuserdata/jared.xcuserdatad/xcschemes/xcschememanagement.plist b/iOS/ItemSense/ItemSense.xcodeproj/xcuserdata/jared.xcuserdatad/xcschemes/xcschememanagement.plist new file mode 100644 index 0000000..4a220d0 --- /dev/null +++ b/iOS/ItemSense/ItemSense.xcodeproj/xcuserdata/jared.xcuserdatad/xcschemes/xcschememanagement.plist @@ -0,0 +1,14 @@ + + + + + SchemeUserState + + ItemSense.xcscheme_^#shared#^_ + + orderHint + 0 + + + + diff --git a/iOS/ItemSense/ItemSense/Assets.xcassets/AccentColor.colorset/Contents.json b/iOS/ItemSense/ItemSense/Assets.xcassets/AccentColor.colorset/Contents.json new file mode 100644 index 0000000..eb87897 --- /dev/null +++ b/iOS/ItemSense/ItemSense/Assets.xcassets/AccentColor.colorset/Contents.json @@ -0,0 +1,11 @@ +{ + "colors" : [ + { + "idiom" : "universal" + } + ], + "info" : { + "author" : "xcode", + "version" : 1 + } +} diff --git a/iOS/ItemSense/ItemSense/Assets.xcassets/AppIcon.appiconset/Contents.json b/iOS/ItemSense/ItemSense/Assets.xcassets/AppIcon.appiconset/Contents.json new file mode 100644 index 0000000..2305880 --- /dev/null +++ b/iOS/ItemSense/ItemSense/Assets.xcassets/AppIcon.appiconset/Contents.json @@ -0,0 +1,35 @@ +{ + "images" : [ + { + "idiom" : "universal", + "platform" : "ios", + "size" : "1024x1024" + }, + { + "appearances" : [ + { + "appearance" : "luminosity", + "value" : "dark" + } + ], + "idiom" : "universal", + "platform" : "ios", + "size" : "1024x1024" + }, + { + "appearances" : [ + { + "appearance" : "luminosity", + "value" : "tinted" + } + ], + "idiom" : "universal", + "platform" : "ios", + "size" : "1024x1024" + } + ], + "info" : { + "author" : "xcode", + "version" : 1 + } +} diff --git a/iOS/ItemSense/ItemSense/Assets.xcassets/Contents.json b/iOS/ItemSense/ItemSense/Assets.xcassets/Contents.json new file mode 100644 index 0000000..73c0059 --- /dev/null +++ b/iOS/ItemSense/ItemSense/Assets.xcassets/Contents.json @@ -0,0 +1,6 @@ +{ + "info" : { + "author" : "xcode", + "version" : 1 + } +} diff --git a/iOS/ItemSense/ItemSense/Camera/CameraManager.swift b/iOS/ItemSense/ItemSense/Camera/CameraManager.swift new file mode 100644 index 0000000..4f1bc93 --- /dev/null +++ b/iOS/ItemSense/ItemSense/Camera/CameraManager.swift @@ -0,0 +1,180 @@ +// +// CameraManager.swift +// ItemSense +// +// Created by Jared Evans on 1/21/26. +// + +import AVFoundation +import UIKit +import Combine + + +class CameraManager: NSObject, ObservableObject { + @Published var session = AVCaptureSession() + @Published var alert: AlertError? + @Published var isAuthorizationGranted = false + + // Delegate to send frames or captured images back + @Published var currentImage: UIImage? + + private let sessionQueue = DispatchQueue(label: "com.itemsense.sessionQueue") + private var videoOutput = AVCaptureVideoDataOutput() + + enum Status { + case unconfigured + case configured + case unauthorized + case failed + } + + @Published var status = Status.unconfigured + + override init() { + super.init() + checkPermissions() + sessionQueue.async { + self.configureSession() + self.session.startRunning() + } + } + + func checkPermissions() { + switch AVCaptureDevice.authorizationStatus(for: .video) { + case .notDetermined: + sessionQueue.suspend() + AVCaptureDevice.requestAccess(for: .video) { authorized in + if !authorized { + DispatchQueue.main.async { + self.status = .unauthorized + self.set(error: .denied) + } + } + self.sessionQueue.resume() + } + case .restricted: + status = .unauthorized + set(error: .restricted) + case .denied: + status = .unauthorized + set(error: .denied) + case .authorized: + break + @unknown default: + status = .unauthorized + set(error: .unknown) + } + } + + private func configureSession() { + guard status == .unconfigured else { return } + + session.beginConfiguration() + session.sessionPreset = .photo + + guard let camera = AVCaptureDevice.default(.builtInWideAngleCamera, for: .video, position: .back) else { + set(error: .cameraUnavailable) + DispatchQueue.main.async { + self.status = .failed + } + session.commitConfiguration() + return + } + + do { + let cameraInput = try AVCaptureDeviceInput(device: camera) + if session.canAddInput(cameraInput) { + session.addInput(cameraInput) + } else { + set(error: .cannotAddInput) + DispatchQueue.main.async { + self.status = .failed + } + session.commitConfiguration() + return + } + } catch { + set(error: .createCaptureInput(error)) + DispatchQueue.main.async { + self.status = .failed + } + session.commitConfiguration() + return + } + + if session.canAddOutput(videoOutput) { + session.addOutput(videoOutput) + videoOutput.videoSettings = [kCVPixelBufferPixelFormatTypeKey as String: Int(kCVPixelFormatType_32BGRA)] + videoOutput.setSampleBufferDelegate(self, queue: sessionQueue) + } else { + set(error: .cannotAddOutput) + DispatchQueue.main.async { + self.status = .failed + } + session.commitConfiguration() + return + } + + DispatchQueue.main.async { + self.status = .configured + } + session.commitConfiguration() + } + + func set(error: CameraError?) { + DispatchQueue.main.async { + if let error = error { + self.alert = AlertError(title: "Camera Error", message: error.description) + } + } + } +} + +extension CameraManager: AVCaptureVideoDataOutputSampleBufferDelegate { + func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) { + guard let pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else { return } + + let ciImage = CIImage(cvPixelBuffer: pixelBuffer) + let context = CIContext() + + // Correct orientation (back camera typically needs 90 deg rotation on portrait) + // For simplicity we deliver the image and let the UI handle valid orientation or simple display + // Note: Real-world apps need careful orientation handling. + + if let cgImage = context.createCGImage(ciImage, from: ciImage.extent) { + let uiImage = UIImage(cgImage: cgImage, scale: 1.0, orientation: .right) + DispatchQueue.main.async { + self.currentImage = uiImage + } + } + } +} + +// Error Handling +struct AlertError: Identifiable { + let id = UUID() + let title: String + let message: String +} + +enum CameraError: Error { + case cameraUnavailable + case cannotAddInput + case cannotAddOutput + case createCaptureInput(Error) + case denied + case restricted + case unknown + + var description: String { + switch self { + case .cameraUnavailable: return "Camera unavailable" + case .cannotAddInput: return "Cannot add input" + case .cannotAddOutput: return "Cannot add output" + case .createCaptureInput(let error): return "Creating input failed: \(error.localizedDescription)" + case .denied: return "Camera access denied" + case .restricted: return "Camera access restricted" + case .unknown: return "Unknown error" + } + } +} diff --git a/iOS/ItemSense/ItemSense/ContentView.swift b/iOS/ItemSense/ItemSense/ContentView.swift new file mode 100644 index 0000000..8cf6595 --- /dev/null +++ b/iOS/ItemSense/ItemSense/ContentView.swift @@ -0,0 +1,111 @@ +// +// ContentView.swift +// ItemSense +// +// Created by Jared Evans on 1/21/26. +// + +import SwiftUI + +struct ContentView: View { + @StateObject private var cameraManager = CameraManager() + @State private var descriptionText: String = "Ready to scan..." + @State private var isProcessing = false + @State private var showWebView = false + @State private var amazonURL: String = "" + + private let openAIService = OpenAIService() + + var body: some View { + ZStack { + // Camera Preview + CameraPreview(session: cameraManager.session) + .ignoresSafeArea() + + VStack { + // Top: Description Area + VStack { + ScrollView { + Text(descriptionText) + .padding() + .frame(maxWidth: .infinity, alignment: .leading) + .foregroundStyle(.white) + } + .frame(height: 150) + .background(.ultraThinMaterial) + .clipShape(RoundedRectangle(cornerRadius: 15)) + .padding() + } + + Spacer() + + // Bottom: Controls + HStack { + if isProcessing { + ProgressView() + .progressViewStyle(CircularProgressViewStyle(tint: .white)) + .scaleEffect(1.5) + } else { + Button(action: captureAndAnalyze) { + Circle() + .strokeBorder(.white, lineWidth: 3) + .background(Circle().fill(.red)) + .frame(width: 70, height: 70) + } + } + } + .padding(.bottom, 30) + } + } + .sheet(isPresented: $showWebView) { + WebView(urlString: amazonURL) + } + .alert(item: $cameraManager.alert) { alert in + Alert(title: Text(alert.title), message: Text(alert.message), dismissButton: .default(Text("OK"))) + } + } + + private func captureAndAnalyze() { + guard let image = cameraManager.currentImage else { + descriptionText = "No image available from camera." + return + } + + isProcessing = true + descriptionText = "Analyzing..." + + // Convert UIImage to Base64 + guard let imageData = image.jpegData(compressionQuality: 0.5) else { + descriptionText = "Failed to process image data." + isProcessing = false + return + } + + let base64String = imageData.base64EncodedString() + + Task { + do { + let analysis = try await openAIService.analyzeImage(base64Image: base64String) + + await MainActor.run { + self.descriptionText = analysis.description + if !analysis.searchTerm.isEmpty { + let query = analysis.searchTerm.replacingOccurrences(of: " ", with: "+") + self.amazonURL = "https://www.amazon.com/s?k=\(query)" + self.showWebView = true + } + self.isProcessing = false + } + } catch { + await MainActor.run { + self.descriptionText = "Error: \(error.localizedDescription)" + self.isProcessing = false + } + } + } + } +} + +#Preview { + ContentView() +} diff --git a/iOS/ItemSense/ItemSense/ItemSenseApp.swift b/iOS/ItemSense/ItemSense/ItemSenseApp.swift new file mode 100644 index 0000000..4c38e21 --- /dev/null +++ b/iOS/ItemSense/ItemSense/ItemSenseApp.swift @@ -0,0 +1,17 @@ +// +// ItemSenseApp.swift +// ItemSense +// +// Created by Jared Evans on 1/21/26. +// + +import SwiftUI + +@main +struct ItemSenseApp: App { + var body: some Scene { + WindowGroup { + ContentView() + } + } +} diff --git a/iOS/ItemSense/ItemSense/Models/ItemAnalysis.swift b/iOS/ItemSense/ItemSense/Models/ItemAnalysis.swift new file mode 100644 index 0000000..0bce29c --- /dev/null +++ b/iOS/ItemSense/ItemSense/Models/ItemAnalysis.swift @@ -0,0 +1,18 @@ +// +// ItemAnalysis.swift +// ItemSense +// +// Created by Jared Evans on 1/21/26. +// + +import Foundation + +struct ItemAnalysis: Codable { + let description: String + let searchTerm: String + + enum CodingKeys: String, CodingKey { + case description + case searchTerm = "search_term" + } +} diff --git a/iOS/ItemSense/ItemSense/Secrets.swift b/iOS/ItemSense/ItemSense/Secrets.swift new file mode 100644 index 0000000..92415d1 --- /dev/null +++ b/iOS/ItemSense/ItemSense/Secrets.swift @@ -0,0 +1,13 @@ +// +// Secrets.swift +// ItemSense +// +// Created by Jared Evans on 1/21/26. +// + +import Foundation + +struct Secrets { + // TODO: Replace with your actual OpenAI API Key + static let openAIAPIKey = "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" +} diff --git a/iOS/ItemSense/ItemSense/Services/OpenAIService.swift b/iOS/ItemSense/ItemSense/Services/OpenAIService.swift new file mode 100644 index 0000000..0dd3723 --- /dev/null +++ b/iOS/ItemSense/ItemSense/Services/OpenAIService.swift @@ -0,0 +1,104 @@ +// +// OpenAIService.swift +// ItemSense +// +// Created by Jared Evans on 1/21/26. +// + +import Foundation + +class OpenAIService { + + enum ServiceError: Error { + case invalidURL + case invalidResponse + case noData + case decodingError(Error) + case apiError(String) + } + + private let apiKey: String + + init(apiKey: String = Secrets.openAIAPIKey) { + self.apiKey = apiKey + } + + func analyzeImage(base64Image: String) async throws -> ItemAnalysis { + guard let url = URL(string: "https://api.openai.com/v1/chat/completions") else { + throw ServiceError.invalidURL + } + + var request = URLRequest(url: url) + request.httpMethod = "POST" + request.addValue("Bearer \(apiKey)", forHTTPHeaderField: "Authorization") + request.addValue("application/json", forHTTPHeaderField: "Content-Type") + + let promptText = """ + Identify the main item in the foreground, including the brand name if visible. Ignore the background and any people present. Return a JSON object with two keys: 'description' (a brief description of the item including brand) and 'search_term' (keywords to search for this item on Amazon, including brand). Return ONLY the JSON. Do not wrap in markdown code blocks. + """ + + // This payload structure mimics the python script's logic + let payload: [String: Any] = [ + "model": "gpt-4o-mini", + "messages": [ + [ + "role": "user", + "content": [ + ["type": "text", "text": promptText], + [ + "type": "image_url", + "image_url": [ + "url": "data:image/jpeg;base64,\(base64Image)" + ] + ] + ] + ] + ], + "max_tokens": 300 + ] + + request.httpBody = try JSONSerialization.data(withJSONObject: payload) + + let (data, response) = try await URLSession.shared.data(for: request) + + guard let httpResponse = response as? HTTPURLResponse, (200...299).contains(httpResponse.statusCode) else { + if let errorText = String(data: data, encoding: .utf8) { + throw ServiceError.apiError("Status: \((response as? HTTPURLResponse)?.statusCode ?? 0), Body: \(errorText)") + } + throw ServiceError.apiError("Status: \((response as? HTTPURLResponse)?.statusCode ?? 0), No Body") + } + + do { + let apiResponse = try JSONDecoder().decode(OpenAIResponse.self, from: data) + guard let content = apiResponse.choices.first?.message.content else { + throw ServiceError.noData + } + + // Clean up content if it contains markdown code blocks + let cleanContent = content + .replacingOccurrences(of: "```json", with: "") + .replacingOccurrences(of: "```", with: "") + .trimmingCharacters(in: .whitespacesAndNewlines) + + guard let jsonData = cleanContent.data(using: .utf8) else { + throw ServiceError.decodingError(NSError(domain: "DataError", code: 0)) + } + + return try JSONDecoder().decode(ItemAnalysis.self, from: jsonData) + + } catch { + throw ServiceError.decodingError(error) + } + } +} + +// Helper structs for OpenAI Response +struct OpenAIResponse: Decodable { + struct Choice: Decodable { + struct Message: Decodable { + let content: String + } + let message: Message + } + let choices: [Choice] +} diff --git a/iOS/ItemSense/ItemSense/Views/CameraPreview.swift b/iOS/ItemSense/ItemSense/Views/CameraPreview.swift new file mode 100644 index 0000000..b12ae4e --- /dev/null +++ b/iOS/ItemSense/ItemSense/Views/CameraPreview.swift @@ -0,0 +1,33 @@ +// +// CameraPreview.swift +// ItemSense +// +// Created by Jared Evans on 1/21/26. +// + +import SwiftUI +import AVFoundation + +struct CameraPreview: UIViewRepresentable { + class VideoPreviewView: UIView { + override class var layerClass: AnyClass { + AVCaptureVideoPreviewLayer.self + } + + var videoPreviewLayer: AVCaptureVideoPreviewLayer { + return layer as! AVCaptureVideoPreviewLayer + } + } + + let session: AVCaptureSession + + func makeUIView(context: Context) -> VideoPreviewView { + let view = VideoPreviewView() + + view.videoPreviewLayer.session = session + view.videoPreviewLayer.videoGravity = .resizeAspectFill + return view + } + + func updateUIView(_ uiView: VideoPreviewView, context: Context) { } +} diff --git a/iOS/ItemSense/ItemSense/Views/WebView.swift b/iOS/ItemSense/ItemSense/Views/WebView.swift new file mode 100644 index 0000000..9f92a6f --- /dev/null +++ b/iOS/ItemSense/ItemSense/Views/WebView.swift @@ -0,0 +1,24 @@ +// +// WebView.swift +// ItemSense +// +// Created by Jared Evans on 1/21/26. +// + +import SwiftUI +import WebKit + +struct WebView: UIViewRepresentable { + let urlString: String + + func makeUIView(context: Context) -> WKWebView { + return WKWebView() + } + + func updateUIView(_ uiView: WKWebView, context: Context) { + if let url = URL(string: urlString) { + let request = URLRequest(url: url) + uiView.load(request) + } + } +} diff --git a/iOS/ItemSense/check_build.sh b/iOS/ItemSense/check_build.sh new file mode 100755 index 0000000..022406d --- /dev/null +++ b/iOS/ItemSense/check_build.sh @@ -0,0 +1,27 @@ +#!/bin/zsh +set -o pipefail # Fail if xcodebuild fails, even with xcbeautify + +# --- Configuration --- +SCHEME="ItemSense" +DEVICE_NAME="iPhone 17 Pro" +BUILD_PATH="./build" + +echo "🔍 Checking compilation for $SCHEME..." + +# Build Only (No Install/Launch) +# We use 'env -u' to hide Homebrew variables +# We use '-derivedDataPath' to keep it isolated +env -u CC -u CXX -u LIBCLANG_PATH xcodebuild \ + -scheme "$SCHEME" \ + -destination "platform=iOS Simulator,name=$DEVICE_NAME" \ + -configuration Debug \ + -derivedDataPath "$BUILD_PATH" \ + build | xcbeautify + +# Check exit code of the pipeline +if [ $? -eq 0 ]; then + echo "✅ Build Succeeded. No errors found." +else + echo "❌ Build Failed." + exit 1 +fi diff --git a/AGENTS.md b/python/AGENTS.md similarity index 100% rename from AGENTS.md rename to python/AGENTS.md diff --git a/CLAUDE.md b/python/CLAUDE.md similarity index 100% rename from CLAUDE.md rename to python/CLAUDE.md diff --git a/PROMPT_build.md b/python/PROMPT_build.md similarity index 100% rename from PROMPT_build.md rename to python/PROMPT_build.md diff --git a/PROMPT_plan.md b/python/PROMPT_plan.md similarity index 100% rename from PROMPT_plan.md rename to python/PROMPT_plan.md diff --git a/README.md b/python/README.md similarity index 100% rename from README.md rename to python/README.md diff --git a/implementation_plan.md b/python/implementation_plan.md similarity index 100% rename from implementation_plan.md rename to python/implementation_plan.md diff --git a/main.py b/python/main.py similarity index 100% rename from main.py rename to python/main.py diff --git a/requirements.txt b/python/requirements.txt similarity index 100% rename from requirements.txt rename to python/requirements.txt diff --git a/scripts/ralph-loop-codex.sh b/python/scripts/ralph-loop-codex.sh similarity index 100% rename from scripts/ralph-loop-codex.sh rename to python/scripts/ralph-loop-codex.sh diff --git a/scripts/ralph-loop.sh b/python/scripts/ralph-loop.sh similarity index 100% rename from scripts/ralph-loop.sh rename to python/scripts/ralph-loop.sh diff --git a/specs/001-core-ui-camera.md b/python/specs/001-core-ui-camera.md similarity index 100% rename from specs/001-core-ui-camera.md rename to python/specs/001-core-ui-camera.md diff --git a/specs/002-openai-integration.md b/python/specs/002-openai-integration.md similarity index 100% rename from specs/002-openai-integration.md rename to python/specs/002-openai-integration.md diff --git a/specs/003-result-display.md b/python/specs/003-result-display.md similarity index 100% rename from specs/003-result-display.md rename to python/specs/003-result-display.md diff --git a/task.md b/python/task.md similarity index 100% rename from task.md rename to python/task.md diff --git a/walkthrough.md b/python/walkthrough.md similarity index 100% rename from walkthrough.md rename to python/walkthrough.md