Port ItemSense to iOS

This commit is contained in:
2026-01-21 15:41:18 -05:00
parent 3a98051de1
commit da6b6ddcd3
32 changed files with 957 additions and 0 deletions

18
.gitignore vendored
View File

@@ -4,3 +4,21 @@ __pycache__/
*.pyc *.pyc
.DS_Store .DS_Store
.specify/ .specify/
# iOS / Xcode
build/
DerivedData/
*.moved-aside
*.rxproj
*.tps
*.xcodeproj/project.xcworkspace
*.xcodeproj/xcuserdata
*.xcworkspace/xcuserdata
*.xccheckout
*.xcscmblueprint
# CocoaPods
Pods/
iOS/ItemSense/build/

View File

@@ -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 = "<group>";
};
/* 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 = "<group>";
};
7F1445BF2F216C600065C89B /* Products */ = {
isa = PBXGroup;
children = (
7F1445BE2F216C600065C89B /* ItemSense.app */,
);
name = Products;
sourceTree = "<group>";
};
/* 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 */;
}

View File

@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<Workspace
version = "1.0">
<FileRef
location = "self:">
</FileRef>
</Workspace>

View File

@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>SchemeUserState</key>
<dict>
<key>ItemSense.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>0</integer>
</dict>
</dict>
</dict>
</plist>

View File

@@ -0,0 +1,11 @@
{
"colors" : [
{
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}

View File

@@ -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
}
}

View File

@@ -0,0 +1,6 @@
{
"info" : {
"author" : "xcode",
"version" : 1
}
}

View File

@@ -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"
}
}
}

View File

@@ -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()
}

View File

@@ -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()
}
}
}

View File

@@ -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"
}
}

View File

@@ -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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"
}

View File

@@ -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]
}

View File

@@ -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) { }
}

View File

@@ -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)
}
}
}

27
iOS/ItemSense/check_build.sh Executable file
View File

@@ -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