Skip to content
This repository has been archived by the owner on Jan 24, 2024. It is now read-only.

Init Commit for iOS AICamera Demo #26

Merged
merged 12 commits into from
Dec 7, 2017
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
478 changes: 478 additions & 0 deletions Demo/iOS/AICamera/AICamera.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions Demo/iOS/AICamera/AICamera/AICamera-Bridging-Header.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//
// Use this file to import your target's public headers that you would like to expose to Swift.
//

#import "ImageRecognizerPaddleWrapper.h"
#import "SSDData.h"
47 changes: 47 additions & 0 deletions Demo/iOS/AICamera/AICamera/AppDelegate.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//
// AppDelegate.swift
// SSDDemo
//
// Created by Nicky Chan on 11/6/17.
// Copyright © 2017 PaddlePaddle. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
UIApplication.shared.isIdleTimerDisabled = true
return true
}

func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}

func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}

func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


}

Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
{
"images" : [
{
"idiom" : "iphone",
"size" : "29x29",
"scale" : "2x"
},
{
"idiom" : "iphone",
"size" : "29x29",
"scale" : "3x"
},
{
"idiom" : "iphone",
"size" : "40x40",
"scale" : "2x"
},
{
"idiom" : "iphone",
"size" : "40x40",
"scale" : "3x"
},
{
"idiom" : "iphone",
"size" : "60x60",
"scale" : "2x"
},
{
"idiom" : "iphone",
"size" : "60x60",
"scale" : "3x"
},
{
"idiom" : "ipad",
"size" : "29x29",
"scale" : "1x"
},
{
"idiom" : "ipad",
"size" : "29x29",
"scale" : "2x"
},
{
"idiom" : "ipad",
"size" : "40x40",
"scale" : "1x"
},
{
"idiom" : "ipad",
"size" : "40x40",
"scale" : "2x"
},
{
"idiom" : "ipad",
"size" : "76x76",
"scale" : "1x"
},
{
"idiom" : "ipad",
"size" : "76x76",
"scale" : "2x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
27 changes: 27 additions & 0 deletions Demo/iOS/AICamera/AICamera/Base.lproj/LaunchScreen.storyboard
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="11134" systemVersion="15F34" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="01J-lp-oVM">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="11106"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
<!--View Controller-->
<scene sceneID="EHf-IW-A2E">
<objects>
<viewController id="01J-lp-oVM" sceneMemberID="viewController">
<layoutGuides>
<viewControllerLayoutGuide type="top" id="Llm-lL-Icb"/>
<viewControllerLayoutGuide type="bottom" id="xb3-aO-Qok"/>
</layoutGuides>
<view key="view" contentMode="scaleToFill" id="Ze5-6b-2t3">
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
</view>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="iYj-Kq-Ea1" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="53" y="375"/>
</scene>
</scenes>
</document>
26 changes: 26 additions & 0 deletions Demo/iOS/AICamera/AICamera/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="11134" systemVersion="15F34" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="11106"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
<!--View Controller-->
<scene sceneID="tne-QT-ifu">
<objects>
<viewController id="BYZ-38-t0r" customClass="ViewController" customModuleProvider="target" sceneMemberID="viewController">
<layoutGuides>
<viewControllerLayoutGuide type="top" id="y3c-jy-aDJ"/>
<viewControllerLayoutGuide type="bottom" id="wfy-db-euE"/>
</layoutGuides>
<view key="view" contentMode="scaleToFill" id="8bC-Xf-vdC">
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
</view>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
</objects>
</scene>
</scenes>
</document>
37 changes: 37 additions & 0 deletions Demo/iOS/AICamera/AICamera/ImageRecognizer.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//
// ImageRecognizer.swift
// SSDDemo
//
// Created by Nicky Chan on 11/7/17.
// Copyright © 2017 PaddlePaddle. All rights reserved.
//

import Foundation

protocol ImageRecognizerDelegate {
func imageRecognizedSuccess(_ result: SSDData)
func imageRecognizedError()
}

class ImageRecognizer {

var imageRecognizer: ImageRecognizerPaddleWrapper?

init() {
imageRecognizer = ImageRecognizerPaddleWrapper()
}

func inference(imageBuffer: UnsafeMutablePointer<UInt8>!, width: Int32, height: Int32) -> NSMutableArray! {

return imageRecognizer?.inference(imageBuffer, withHeight: height, withWidth: width)
}

func release() {
imageRecognizer?.destroy()
}

}




Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove these blank lines.

21 changes: 21 additions & 0 deletions Demo/iOS/AICamera/AICamera/ImageRecognizerPaddleWrapper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// ImageRecognizerPaddleWrapper.h
// AICamera
//
// Created by Nicky Chan on 11/9/17.
// Copyright © 2017 PaddlePaddle. All rights reserved.
//

#ifndef ImageRecognizerPaddleWrapper_h
#define ImageRecognizerPaddleWrapper_h

#import <Foundation/Foundation.h>

@interface ImageRecognizerPaddleWrapper : NSObject

- (NSMutableArray*)inference:(unsigned char *)pixels withHeight:(int)height withWidth:(int)width;
- (void)destroy;

@end

#endif /* ImageRecognizerPaddleWrapper_h */
85 changes: 85 additions & 0 deletions Demo/iOS/AICamera/AICamera/ImageRecognizerPaddleWrapper.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
//
// ImageRecognizerPaddleWrapper.m
// SSDDemo
//
// Created by Nicky Chan on 11/7/17.
// Copyright © 2017 PaddlePaddle. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "ImageRecognizerPaddleWrapper.h"
#include "paddle_image_recognizer.h"
#include "SSDData.h"

@interface ImageRecognizerPaddleWrapper () {
ImageRecognizer recognizer;
}
@end

@implementation ImageRecognizerPaddleWrapper

static NSString * kLabels [21] = {
@"background", @"aeroplane", @"bicycle", @"background", @"boat",
@"bottle", @"bus", @"car", @"cat", @"chair",
@"cow", @"diningtable", @"dog", @"horse", @"motorbike",
@"person", @"pottedplant", @"sheep", @"sofa", @"train",
@"tvmonitor"
};

static float kFilterScore = 0.3;

- (instancetype)init {
self = [super init];
if (self)
{
int normedHeight = 300;
int normedWidth = 300;
int channel = 3;
const std::vector<float> means({104, 117, 124});

NSBundle* bundle = [NSBundle mainBundle];
NSString* resourceDirectoryPath = [bundle bundlePath];
NSString* path = [resourceDirectoryPath stringByAppendingString: @"/vgg_ssd_net.paddle"];

self->recognizer.init([path UTF8String], normedHeight, normedWidth, channel, means);

}
return self;
}

- (NSMutableArray*)inference:(unsigned char *)pixels withHeight:(int)height withWidth:(int)width {
ImageRecognizer::Result result;
int channel = 4;
self->recognizer.infer(pixels, height, width, channel, result);

NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:result.height];
int w = result.width;

NSLog(@"result height = %d", result.height);

for (int i = 0; i < result.height; i++) {
float score = result.data[i * w + 2];
if (score < kFilterScore) continue;

SSDData *ssdData = [[SSDData alloc] init];
ssdData.label = kLabels[(int) result.data[i * w + 1]];
ssdData.accuracy = score;
ssdData.xmin = result.data[i * w + 3];
ssdData.ymin = result.data[i * w + 4];
ssdData.xmax = result.data[i * w + 5];
ssdData.ymax = result.data[i * w + 6];

// NSLog(@"label = %@, acc = %f, xmin = %f, xmax = %f, ymin = %f, ymax = %f", ssdData.label, ssdData.accuracy, ssdData.xmin, ssdData.xmax, ssdData.ymin, ssdData.ymax);

[array addObject:ssdData];
}

return array;
}

- (void)destroy {
self->recognizer.release();
}


@end
51 changes: 51 additions & 0 deletions Demo/iOS/AICamera/AICamera/Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?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>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleDisplayName</key>
<string>AICamera</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>NSCameraUsageDescription</key>
<string>Requires camera access</string>
<key>UILaunchStoryboardName</key>
<string>LaunchScreen</string>
<key>UIMainStoryboardFile</key>
<string>Main</string>
<key>UIRequiredDeviceCapabilities</key>
<array>
<string>armv7</string>
</array>
<key>UIRequiresFullScreen</key>
<true/>
<key>UIStatusBarHidden</key>
<true/>
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
</array>
<key>UISupportedInterfaceOrientations~ipad</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationPortraitUpsideDown</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
</dict>
</plist>