提交 f5244eef 编写于 作者: L Liangliang He

Merge branch 'demo' into 'master'

add android demo

See merge request !601
*.iml
.gradle
/local.properties
/.idea/workspace.xml
/.idea/libraries
.DS_Store
/build
/captures
.externalNativeBuild
# mace demo使用方法
* 使用前需要生成静态库和头文件,具体参考[文档](docs)
* 把mace/public目录下的mace.h和mace_runtime.h拷贝到macelibrary/src/main/cpp/include下面
* 把生成的mace/codegen/engine/mace_engine_factory.h拷贝到macelibrary/src/main/cpp/include下面
* 静态库的路径是在mace/build/demo_app_models/lib/下
* 使用android studio 导入项目,然后运行install run
* 还可以使用gradle命令(需要安装gradle)生成apk 具体命令例如:./gradlew assemble(或者Release|Debug)
## 交流与反馈
* 欢迎通过Github Issues提交问题报告与建议
* QQ群: 756046893
## License
[Apache License 2.0](LICENSE).
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
defaultConfig {
applicationId "com.xiaomi.mace.demo"
minSdkVersion 23
targetSdkVersion 26
versionCode 1
versionName "1.0"
}
signingConfigs {
release {
storeFile file("mace_demo.jks")
keyAlias "keyAlias"
keyPassword "maceAndroid"
storePassword "maceAndroid"
}
debug {
storeFile file("mace_demo.jks")
keyAlias "keyAlias"
keyPassword "maceAndroid"
storePassword "maceAndroid"
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.release
}
}
flavorDimensions "mace"
productFlavors {
app {
dimension "mace"
}
}
sourceSets {
main {
jniLibs.srcDirs = ["libs"]
jni.srcDirs = ['src/main/jni', 'src/main/jni/']
}
}
applicationVariants.all { variant ->
if (variant.buildType.name != "release") {
return
}
def outputFolder = file("build/output/apk")
variant.outputs.all {output ->
def name = output.outputFile.name
def newName = "Mace_" + new Date().format("yyyyMMdd_HHmm") + "_" + defaultConfig.versionName + "_" + variant.flavorName + ".apk"
copy {
from output.outputFile.parent
into outputFolder
include name
rename(name, newName)
}
}
}
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.0'
implementation project(':macelibrary')
implementation 'org.greenrobot:eventbus:3.1.1'
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xiaomi.mace.demo">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<application
android:name=".MaceApp"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".CameraActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
\ No newline at end of file
package(default_visibility = ["//visibility:public"])
licenses(["notice"]) # Apache 2.0
# It is necessary to use this filegroup rather than globbing the files in this
# folder directly the examples/android:tensorflow_demo target due to the fact
# that assets_dir is necessarily set to "" there (to allow using other
# arbitrary targets as assets).
filegroup(
name = "asset_files",
srcs = glob(
["**/*"],
exclude = ["BUILD"],
),
)
background
tench
goldfish
great white shark
tiger shark
hammerhead
electric ray
stingray
cock
hen
ostrich
brambling
goldfinch
house finch
junco
indigo bunting
robin
bulbul
jay
magpie
chickadee
water ouzel
kite
bald eagle
vulture
great grey owl
European fire salamander
common newt
eft
spotted salamander
axolotl
bullfrog
tree frog
tailed frog
loggerhead
leatherback turtle
mud turtle
terrapin
box turtle
banded gecko
common iguana
American chameleon
whiptail
agama
frilled lizard
alligator lizard
Gila monster
green lizard
African chameleon
Komodo dragon
African crocodile
American alligator
triceratops
thunder snake
ringneck snake
hognose snake
green snake
king snake
garter snake
water snake
vine snake
night snake
boa constrictor
rock python
Indian cobra
green mamba
sea snake
horned viper
diamondback
sidewinder
trilobite
harvestman
scorpion
black and gold garden spider
barn spider
garden spider
black widow
tarantula
wolf spider
tick
centipede
black grouse
ptarmigan
ruffed grouse
prairie chicken
peacock
quail
partridge
African grey
macaw
sulphur-crested cockatoo
lorikeet
coucal
bee eater
hornbill
hummingbird
jacamar
toucan
drake
red-breasted merganser
goose
black swan
tusker
echidna
platypus
wallaby
koala
wombat
jellyfish
sea anemone
brain coral
flatworm
nematode
conch
snail
slug
sea slug
chiton
chambered nautilus
Dungeness crab
rock crab
fiddler crab
king crab
American lobster
spiny lobster
crayfish
hermit crab
isopod
white stork
black stork
spoonbill
flamingo
little blue heron
American egret
bittern
crane
limpkin
European gallinule
American coot
bustard
ruddy turnstone
red-backed sandpiper
redshank
dowitcher
oystercatcher
pelican
king penguin
albatross
grey whale
killer whale
dugong
sea lion
Chihuahua
Japanese spaniel
Maltese dog
Pekinese
Shih-Tzu
Blenheim spaniel
papillon
toy terrier
Rhodesian ridgeback
Afghan hound
basset
beagle
bloodhound
bluetick
black-and-tan coonhound
Walker hound
English foxhound
redbone
borzoi
Irish wolfhound
Italian greyhound
whippet
Ibizan hound
Norwegian elkhound
otterhound
Saluki
Scottish deerhound
Weimaraner
Staffordshire bullterrier
American Staffordshire terrier
Bedlington terrier
Border terrier
Kerry blue terrier
Irish terrier
Norfolk terrier
Norwich terrier
Yorkshire terrier
wire-haired fox terrier
Lakeland terrier
Sealyham terrier
Airedale
cairn
Australian terrier
Dandie Dinmont
Boston bull
miniature schnauzer
giant schnauzer
standard schnauzer
Scotch terrier
Tibetan terrier
silky terrier
soft-coated wheaten terrier
West Highland white terrier
Lhasa
flat-coated retriever
curly-coated retriever
golden retriever
Labrador retriever
Chesapeake Bay retriever
German short-haired pointer
vizsla
English setter
Irish setter
Gordon setter
Brittany spaniel
clumber
English springer
Welsh springer spaniel
cocker spaniel
Sussex spaniel
Irish water spaniel
kuvasz
schipperke
groenendael
malinois
briard
kelpie
komondor
Old English sheepdog
Shetland sheepdog
collie
Border collie
Bouvier des Flandres
Rottweiler
German shepherd
Doberman
miniature pinscher
Greater Swiss Mountain dog
Bernese mountain dog
Appenzeller
EntleBucher
boxer
bull mastiff
Tibetan mastiff
French bulldog
Great Dane
Saint Bernard
Eskimo dog
malamute
Siberian husky
dalmatian
affenpinscher
basenji
pug
Leonberg
Newfoundland
Great Pyrenees
Samoyed
Pomeranian
chow
keeshond
Brabancon griffon
Pembroke
Cardigan
toy poodle
miniature poodle
standard poodle
Mexican hairless
timber wolf
white wolf
red wolf
coyote
dingo
dhole
African hunting dog
hyena
red fox
kit fox
Arctic fox
grey fox
tabby
tiger cat
Persian cat
Siamese cat
Egyptian cat
cougar
lynx
leopard
snow leopard
jaguar
lion
tiger
cheetah
brown bear
American black bear
ice bear
sloth bear
mongoose
meerkat
tiger beetle
ladybug
ground beetle
long-horned beetle
leaf beetle
dung beetle
rhinoceros beetle
weevil
fly
bee
ant
grasshopper
cricket
walking stick
cockroach
mantis
cicada
leafhopper
lacewing
dragonfly
damselfly
admiral
ringlet
monarch
cabbage butterfly
sulphur butterfly
lycaenid
starfish
sea urchin
sea cucumber
wood rabbit
hare
Angora
hamster
porcupine
fox squirrel
marmot
beaver
guinea pig
sorrel
zebra
hog
wild boar
warthog
hippopotamus
ox
water buffalo
bison
ram
bighorn
ibex
hartebeest
impala
gazelle
Arabian camel
llama
weasel
mink
polecat
black-footed ferret
otter
skunk
badger
armadillo
three-toed sloth
orangutan
gorilla
chimpanzee
gibbon
siamang
guenon
patas
baboon
macaque
langur
colobus
proboscis monkey
marmoset
capuchin
howler monkey
titi
spider monkey
squirrel monkey
Madagascar cat
indri
Indian elephant
African elephant
lesser panda
giant panda
barracouta
eel
coho
rock beauty
anemone fish
sturgeon
gar
lionfish
puffer
abacus
abaya
academic gown
accordion
acoustic guitar
aircraft carrier
airliner
airship
altar
ambulance
amphibian
analog clock
apiary
apron
ashcan
assault rifle
backpack
bakery
balance beam
balloon
ballpoint
Band Aid
banjo
bannister
barbell
barber chair
barbershop
barn
barometer
barrel
barrow
baseball
basketball
bassinet
bassoon
bathing cap
bath towel
bathtub
beach wagon
beacon
beaker
bearskin
beer bottle
beer glass
bell cote
bib
bicycle-built-for-two
bikini
binder
binoculars
birdhouse
boathouse
bobsled
bolo tie
bonnet
bookcase
bookshop
bottlecap
bow
bow tie
brass
brassiere
breakwater
breastplate
broom
bucket
buckle
bulletproof vest
bullet train
butcher shop
cab
caldron
candle
cannon
canoe
can opener
cardigan
car mirror
carousel
carpenter's kit
carton
car wheel
cash machine
cassette
cassette player
castle
catamaran
CD player
cello
cellular telephone
chain
chainlink fence
chain mail
chain saw
chest
chiffonier
chime
china cabinet
Christmas stocking
church
cinema
cleaver
cliff dwelling
cloak
clog
cocktail shaker
coffee mug
coffeepot
coil
combination lock
computer keyboard
confectionery
container ship
convertible
corkscrew
cornet
cowboy boot
cowboy hat
cradle
crane
crash helmet
crate
crib
Crock Pot
croquet ball
crutch
cuirass
dam
desk
desktop computer
dial telephone
diaper
digital clock
digital watch
dining table
dishrag
dishwasher
disk brake
dock
dogsled
dome
doormat
drilling platform
drum
drumstick
dumbbell
Dutch oven
electric fan
electric guitar
electric locomotive
entertainment center
envelope
espresso maker
face powder
feather boa
file
fireboat
fire engine
fire screen
flagpole
flute
folding chair
football helmet
forklift
fountain
fountain pen
four-poster
freight car
French horn
frying pan
fur coat
garbage truck
gasmask
gas pump
goblet
go-kart
golf ball
golfcart
gondola
gong
gown
grand piano
greenhouse
grille
grocery store
guillotine
hair slide
hair spray
half track
hammer
hamper
hand blower
hand-held computer
handkerchief
hard disc
harmonica
harp
harvester
hatchet
holster
home theater
honeycomb
hook
hoopskirt
horizontal bar
horse cart
hourglass
iPod
iron
jack-o'-lantern
jean
jeep
jersey
jigsaw puzzle
jinrikisha
joystick
kimono
knee pad
knot
lab coat
ladle
lampshade
laptop
lawn mower
lens cap
letter opener
library
lifeboat
lighter
limousine
liner
lipstick
Loafer
lotion
loudspeaker
loupe
lumbermill
magnetic compass
mailbag
mailbox
maillot
maillot
manhole cover
maraca
marimba
mask
matchstick
maypole
maze
measuring cup
medicine chest
megalith
microphone
microwave
military uniform
milk can
minibus
miniskirt
minivan
missile
mitten
mixing bowl
mobile home
Model T
modem
monastery
monitor
moped
mortar
mortarboard
mosque
mosquito net
motor scooter
mountain bike
mountain tent
mouse
mousetrap
moving van
muzzle
nail
neck brace
necklace
nipple
notebook
obelisk
oboe
ocarina
odometer
oil filter
organ
oscilloscope
overskirt
oxcart
oxygen mask
packet
paddle
paddlewheel
padlock
paintbrush
pajama
palace
panpipe
paper towel
parachute
parallel bars
park bench
parking meter
passenger car
patio
pay-phone
pedestal
pencil box
pencil sharpener
perfume
Petri dish
photocopier
pick
pickelhaube
picket fence
pickup
pier
piggy bank
pill bottle
pillow
ping-pong ball
pinwheel
pirate
pitcher
plane
planetarium
plastic bag
plate rack
plow
plunger
Polaroid camera
pole
police van
poncho
pool table
pop bottle
pot
potter's wheel
power drill
prayer rug
printer
prison
projectile
projector
puck
punching bag
purse
quill
quilt
racer
racket
radiator
radio
radio telescope
rain barrel
recreational vehicle
reel
reflex camera
refrigerator
remote control
restaurant
revolver
rifle
rocking chair
rotisserie
rubber eraser
rugby ball
rule
running shoe
safe
safety pin
saltshaker
sandal
sarong
sax
scabbard
scale
school bus
schooner
scoreboard
screen
screw
screwdriver
seat belt
sewing machine
shield
shoe shop
shoji
shopping basket
shopping cart
shovel
shower cap
shower curtain
ski
ski mask
sleeping bag
slide rule
sliding door
slot
snorkel
snowmobile
snowplow
soap dispenser
soccer ball
sock
solar dish
sombrero
soup bowl
space bar
space heater
space shuttle
spatula
speedboat
spider web
spindle
sports car
spotlight
stage
steam locomotive
steel arch bridge
steel drum
stethoscope
stole
stone wall
stopwatch
stove
strainer
streetcar
stretcher
studio couch
stupa
submarine
suit
sundial
sunglass
sunglasses
sunscreen
suspension bridge
swab
sweatshirt
swimming trunks
swing
switch
syringe
table lamp
tank
tape player
teapot
teddy
television
tennis ball
thatch
theater curtain
thimble
thresher
throne
tile roof
toaster
tobacco shop
toilet seat
torch
totem pole
tow truck
toyshop
tractor
trailer truck
tray
trench coat
tricycle
trimaran
tripod
triumphal arch
trolleybus
trombone
tub
turnstile
typewriter keyboard
umbrella
unicycle
upright
vacuum
vase
vault
velvet
vending machine
vestment
viaduct
violin
volleyball
waffle iron
wall clock
wallet
wardrobe
warplane
washbasin
washer
water bottle
water jug
water tower
whiskey jug
whistle
wig
window screen
window shade
Windsor tie
wine bottle
wing
wok
wooden spoon
wool
worm fence
wreck
yawl
yurt
web site
comic book
crossword puzzle
street sign
traffic light
book jacket
menu
plate
guacamole
consomme
hot pot
trifle
ice cream
ice lolly
French loaf
bagel
pretzel
cheeseburger
hotdog
mashed potato
head cabbage
broccoli
cauliflower
zucchini
spaghetti squash
acorn squash
butternut squash
cucumber
artichoke
bell pepper
cardoon
mushroom
Granny Smith
strawberry
orange
lemon
fig
pineapple
banana
jackfruit
custard apple
pomegranate
hay
carbonara
chocolate sauce
dough
meat loaf
pizza
potpie
burrito
red wine
espresso
cup
eggnog
alp
bubble
cliff
coral reef
geyser
lakeside
promontory
sandbar
seashore
valley
volcano
ballplayer
groom
scuba diver
rapeseed
daisy
yellow lady's slipper
corn
acorn
hip
buckeye
coral fungus
agaric
gyromitra
stinkhorn
earthstar
hen-of-the-woods
bolete
ear
toilet tissue
\ No newline at end of file
// Copyright 2018 Xiaomi, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.xiaomi.mace.demo;
import android.os.Handler;
import android.os.HandlerThread;
import android.util.Log;
import com.xiaomi.mace.JniMaceUtils;
import com.xiaomi.mace.demo.camera.MessageEvent;
import com.xiaomi.mace.demo.result.InitData;
import com.xiaomi.mace.demo.result.LabelCache;
import com.xiaomi.mace.demo.result.ResultData;
import org.greenrobot.eventbus.EventBus;
public class AppModel {
private Handler mJniThread;
public static AppModel instance = new AppModel();
private AppModel() {
HandlerThread thread = new HandlerThread("jniThread");
thread.start();
mJniThread = new Handler(thread.getLooper());
}
public void maceMobilenetSetAttrs(final InitData initData) {
mJniThread.post(new Runnable() {
@Override
public void run() {
int result = JniMaceUtils.maceMobilenetSetAttrs(
initData.getOmpNumThreads(), initData.getCpuAffinityPolicy(),
initData.getGpuPerfHint(), initData.getGpuPriorityHint(),
initData.getKernelPath());
Log.i("APPModel", "maceMobilenetSetAttrs result = " + result);
}
});
}
public void maceMobilenetCreateEngine(final InitData initData) {
mJniThread.post(new Runnable() {
@Override
public void run() {
JniMaceUtils.maceMobilenetCreateEngine(initData.getModel(), initData.getDevice());
}
});
}
public void maceMobilenetClassify(final float[] input) {
mJniThread.post(new Runnable() {
@Override
public void run() {
long start = System.currentTimeMillis();
float[] result = JniMaceUtils.maceMobilenetClassify(input);
final ResultData resultData = LabelCache.instance().getResultFirst(result);
resultData.costTime = System.currentTimeMillis() - start;
EventBus.getDefault().post(new MessageEvent.MaceResultEvent(resultData));
}
});
}
}
// Copyright 2018 Xiaomi, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.xiaomi.mace.demo;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import com.xiaomi.mace.demo.camera.CameraEngage;
import com.xiaomi.mace.demo.camera.CameraTextureView;
import com.xiaomi.mace.demo.camera.ContextMenuDialog;
import com.xiaomi.mace.demo.camera.MessageEvent;
import com.xiaomi.mace.demo.result.InitData;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import java.util.Arrays;
import java.util.List;
public class CameraActivity extends Activity implements View.OnClickListener {
CameraEngage mCameraEngage;
ImageView mPictureResult;
Button mSelectMode;
Button mSelectPhoneType;
CameraTextureView mCameraTextureView;
private TextView mResultView;
private InitData initData = new InitData();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_camera);
mPictureResult = findViewById(R.id.iv_picture);
mResultView = findViewById(R.id.tv_show_result);
mCameraTextureView = findViewById(R.id.camera_texture);
mCameraEngage = CameraFactory.genCameEngage(mCameraTextureView);
mSelectMode = findViewById(R.id.tv_select_mode);
mSelectMode.setOnClickListener(this);
mSelectPhoneType = findViewById(R.id.tv_select_phone_type);
mSelectPhoneType.setOnClickListener(this);
initJni();
initView();
}
@Override
protected void onStart() {
super.onStart();
EventBus.getDefault().register(this);
}
@Override
protected void onStop() {
super.onStop();
EventBus.getDefault().unregister(this);
}
private void initView() {
mSelectMode.setText(initData.getModel());
mSelectPhoneType.setText(initData.getDevice());
}
@Override
protected void onResume() {
super.onResume();
mCameraEngage.onResume();
}
@Override
protected void onPause() {
super.onPause();
mCameraEngage.onPause();
}
@Subscribe(threadMode = ThreadMode.MAIN)
public void onPicture(MessageEvent.PicEvent picEvent) {
if (picEvent != null && picEvent.getBitmap() != null) {
mPictureResult.setImageBitmap(picEvent.getBitmap());
}
}
@Subscribe(threadMode = ThreadMode.MAIN)
public void onSetCameraViewSize(MessageEvent.OutputSizeEvent event) {
if (mCameraTextureView != null) {
mCameraTextureView.setRatio(event.width, event.height);
}
}
@Subscribe(threadMode = ThreadMode.MAIN)
public void onResultData(MessageEvent.MaceResultEvent resultData) {
if (resultData != null && resultData.getData() != null) {
String result = resultData.getData().name + "\n"
+ resultData.getData().probability + "\ncost time(ms): "
+ resultData.getData().costTime;
mResultView.setText(result);
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.tv_select_mode:
showSelectMode();
break;
case R.id.tv_select_phone_type:
showPhoneType();
break;
}
}
private void initJni() {
AppModel.instance.maceMobilenetSetAttrs(initData);
AppModel.instance.maceMobilenetCreateEngine(initData);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == Constant.CAMERA_PERMISSION_REQ) {
for (int result : grantResults) {
if (result != PackageManager.PERMISSION_GRANTED) {
mCameraEngage.onResume();
break;
}
}
}
}
private void showPhoneType() {
List<String> menus = Arrays.asList(InitData.DEVICES);
ContextMenuDialog.show(this, menus, new ContextMenuDialog.OnClickItemListener() {
@Override
public void onCLickItem(String content) {
mSelectPhoneType.setText(content);
initData.setDevice(content);
AppModel.instance.maceMobilenetCreateEngine(initData);
}
});
}
private void showSelectMode() {
List<String> menus = Arrays.asList(InitData.MODELS);
ContextMenuDialog.show(this, menus, new ContextMenuDialog.OnClickItemListener() {
@Override
public void onCLickItem(String content) {
mSelectMode.setText(content);
initData.setModel(content);
AppModel.instance.maceMobilenetCreateEngine(initData);
}
});
}
}
// Copyright 2018 Xiaomi, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.xiaomi.mace.demo;
import android.os.Build;
import com.xiaomi.mace.demo.camera.CameraApiLessM;
import com.xiaomi.mace.demo.camera.CameraEngage;
import com.xiaomi.mace.demo.camera.CameraTextureView;
public class CameraFactory {
public static CameraEngage genCameEngage(CameraTextureView textureView) {
CameraEngage cameraEngage;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
// cameraEngage = new CameraApiMoreM(textureView);
cameraEngage = new CameraApiLessM(textureView);
} else {
cameraEngage = new CameraApiLessM(textureView);
}
return cameraEngage;
}
}
// Copyright 2018 Xiaomi, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.xiaomi.mace.demo;
public class Constant {
public static final int CAMERA_PERMISSION_REQ = 1;
}
// Copyright 2018 Xiaomi, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.xiaomi.mace.demo;
import android.app.Application;
import android.os.Handler;
import android.os.HandlerThread;
import com.xiaomi.mace.demo.result.LabelCache;
public class MaceApp extends Application {
public static MaceApp app;
public Handler mBackground;
public Handler mMainHandler;
@Override
public void onCreate() {
super.onCreate();
app = this;
mMainHandler = new Handler(getMainLooper());
HandlerThread thread = new HandlerThread("mace_app");
thread.start();
mBackground = new Handler(thread.getLooper());
mBackground.post(new Runnable() {
@Override
public void run() {
LabelCache.instance();
}
});
}
public Handler mainHandler() {
return mMainHandler;
}
}
// Copyright 2018 Xiaomi, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.xiaomi.mace.demo.camera;
import android.graphics.ImageFormat;
import android.hardware.Camera;
import android.text.TextUtils;
import android.util.Log;
import java.util.List;
public class CameraApiLessM extends CameraEngage implements Camera.AutoFocusCallback {
private Camera mCamera;
public CameraApiLessM(CameraTextureView textureView) {
super(textureView);
}
@Override
public void openCamera(int width, int height) {
if (!checkCameraPermission()) {
return;
}
super.openCamera(width, height);
closeCamera();
String cameraId = getCameraId();
if (TextUtils.isEmpty(cameraId)) {
return;
}
mCamera = Camera.open(Integer.parseInt(cameraId));
setOutputConfig(width, height);
startPreview();
}
@Override
public void autoFocus() {
doAutoFocus();
}
@Override
public void closeCamera() {
if (mCamera != null) {
mCamera.stopPreview();
mCamera.setPreviewCallback(null);
mCamera.release();
mCamera = null;
}
}
@Override
public String getCameraId() {
Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
int cameraFacing = facingFrontPreview() ? Camera.CameraInfo.CAMERA_FACING_FRONT : Camera.CameraInfo.CAMERA_FACING_BACK;
for (int i = 0; i < Camera.getNumberOfCameras(); i++) {
Camera.getCameraInfo(i, cameraInfo);
if (cameraInfo.facing == cameraFacing) {
return String.valueOf(i);
}
}
return "";
}
@Override
public void startPreview() {
try {
doAutoFocus();
mCamera.setPreviewTexture(mSurfaceTexture);
mCamera.startPreview();
mCamera.setDisplayOrientation(90);
} catch (Exception e) {
Log.e(getClass().getName(), "startPreview error = " + e);
}
}
@Override
public void onAutoFocus(boolean success, Camera camera) {
if (success) {
mCamera.cancelAutoFocus();
}
}
private void doAutoFocus() {
try {
mCamera.autoFocus(this);
} catch (Throwable e) {
Log.e(this.getClass().getName(), "auto focus error = " + e);
}
}
private void setOutputConfig(int width, int height) {
Camera.Parameters parameters = mCamera.getParameters();
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
parameters.setPreviewFormat(ImageFormat.JPEG);
parameters.setPictureFormat(ImageFormat.JPEG);
Camera.Size size = getOptimalSize(parameters.getSupportedPreviewSizes(), width, height);
mPreviewWidth = size.width;
mPreviewHeight = size.height;
parameters.setPreviewSize(size.width, size.height);
parameters.setPictureSize(size.width, size.height);
mCamera.setParameters(parameters);
mTextureView.setRatio(size.height, size.width);
}
private Camera.Size getOptimalSize(List<Camera.Size> sizes, int w, int h) {
final double ASPECT_TOLERANCE = 0.1;
double targetRatio = (double) h / w;
Camera.Size optimalSize = null;
double minDiff = Double.MAX_VALUE;
int targetHeight = h;
for (Camera.Size size : sizes) {
double ratio = (double) size.height / size.width;
if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) {
continue;
}
if (Math.abs(size.height - targetHeight) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
if (optimalSize == null) {
minDiff = Double.MAX_VALUE;
for (Camera.Size size : sizes) {
if (Math.abs(size.height - targetHeight) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
}
return optimalSize;
}
}
// Copyright 2018 Xiaomi, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.xiaomi.mace.demo.camera;
import android.content.Context;
import android.graphics.ImageFormat;
import android.graphics.SurfaceTexture;
import android.hardware.camera2.CameraCaptureSession;
import android.hardware.camera2.CameraCaptureSession.CaptureCallback;
import android.hardware.camera2.CameraCharacteristics;
import android.hardware.camera2.CameraDevice;
import android.hardware.camera2.CameraManager;
import android.hardware.camera2.CaptureFailure;
import android.hardware.camera2.CaptureRequest;
import android.hardware.camera2.CaptureResult;
import android.hardware.camera2.TotalCaptureResult;
import android.hardware.camera2.params.StreamConfigurationMap;
import android.media.ImageReader;
import android.os.Handler;
import android.os.HandlerThread;
import android.support.annotation.NonNull;
import android.util.Log;
import android.util.Size;
import android.view.Surface;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class CameraApiMoreM extends CameraEngage {
private CameraManager mCameraManager;
private ImageReader mImageReader;
private CameraCaptureSession mCameraCaptureSession;
private CaptureRequest.Builder mPreviewBuilder;
private Handler mCameraHandler;
private CameraDevice mCameraDevice;
private Size mPreviewSize;
private CameraDevice.StateCallback mStateCallback = new CameraDevice.StateCallback() {
@Override
public void onOpened(@NonNull CameraDevice camera) {
mCameraDevice = camera;
createPreviewSession();
}
@Override
public void onDisconnected(@NonNull CameraDevice camera) {
camera.close();
mCameraDevice = null;
}
@Override
public void onError(@NonNull CameraDevice camera, int error) {
camera.close();
mCameraDevice = null;
}
};
private CameraCaptureSession.CaptureCallback mCaptureCallback = new CaptureCallback() {
@Override
public void onCaptureProgressed(@NonNull CameraCaptureSession session, @NonNull CaptureRequest request, @NonNull CaptureResult partialResult) {
super.onCaptureProgressed(session, request, partialResult);
}
@Override
public void onCaptureCompleted(@NonNull CameraCaptureSession session, @NonNull CaptureRequest request, @NonNull TotalCaptureResult result) {
super.onCaptureCompleted(session, request, result);
}
@Override
public void onCaptureFailed(@NonNull CameraCaptureSession session, @NonNull CaptureRequest request, @NonNull CaptureFailure failure) {
super.onCaptureFailed(session, request, failure);
}
};
private CameraCaptureSession.StateCallback mSessionStateCallback = new CameraCaptureSession.StateCallback() {
@Override
public void onConfigured(@NonNull CameraCaptureSession session) {
if (mCameraDevice == null) {
Log.e("bjh", "device must be not null");
return;
}
mCameraCaptureSession = session;
startPreview();
}
@Override
public void onConfigureFailed(@NonNull CameraCaptureSession session) {
}
};
private void createPreviewSession() {
try {
mSurfaceTexture.setDefaultBufferSize(mPreviewSize.getWidth(), mPreviewSize.getHeight());
Surface surface = new Surface(mSurfaceTexture);
mPreviewBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
mPreviewBuilder.addTarget(surface);
mCameraDevice.createCaptureSession(Arrays.asList(surface), mSessionStateCallback, null);
} catch (Exception e) {
e.printStackTrace();
}
}
public CameraApiMoreM(CameraTextureView textureView) {
super(textureView);
mCameraManager = (CameraManager) textureView.getContext().getSystemService(Context.CAMERA_SERVICE);
if (mCameraManager == null) {
throw new RuntimeException("camera manager must be not null");
}
HandlerThread handlerThread = new HandlerThread("camerabg");
handlerThread.start();
mCameraHandler = new Handler(handlerThread.getLooper());
}
@Override
public void openCamera(int width, int height) {
super.openCamera(width, height);
try {
if (!checkCameraPermission()) {
return;
}
setOutputConfig(width, height);
createImageReader();
mCameraManager.openCamera(getCameraId(), mStateCallback, mCameraHandler);
} catch (Exception e) {
e.printStackTrace();
}
}
private void setOutputConfig(int width, int height) {
try {
for (String cameraId : mCameraManager.getCameraIdList()) {
CameraCharacteristics characteristics = mCameraManager.getCameraCharacteristics(cameraId);
Integer facing = characteristics.get(CameraCharacteristics.LENS_FACING);
if (facing != null && facing == CameraCharacteristics.LENS_FACING_FRONT) {
continue;
}
StreamConfigurationMap map = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
if (map == null) {
continue;
}
Size largest = Collections.max(Arrays.asList(map.getOutputSizes(ImageFormat.JPEG)), new CompareSizesByArea());
mPreviewSize = chooseOptimalSize(map.getOutputSizes(SurfaceTexture.class), width, height, mPreviewWidth, mPreviewHeight, largest);
mPreviewWidth = mPreviewSize.getWidth();
mPreviewHeight = mPreviewSize.getHeight();
mTextureView.setRatio(mPreviewSize.getHeight(), mPreviewSize.getWidth());
break;
}
} catch (Exception e) {
Log.e("bjh", "error " + e);
}
}
private static Size chooseOptimalSize(Size[] choices, int textureViewWidth, int textureViewHeight, int maxWidth, int maxHeight, Size aspectRatio) {
// Collect the supported resolutions that are at least as big as the preview Surface
List<Size> bigEnough = new ArrayList<>();
// Collect the supported resolutions that are smaller than the preview Surface
List<Size> notBigEnough = new ArrayList<>();
int w = aspectRatio.getWidth();
int h = aspectRatio.getHeight();
for (Size option : choices) {
if (option.getWidth() <= maxWidth && option.getHeight() <= maxHeight && option.getHeight() == option.getWidth() * h / w) {
if (option.getWidth() >= textureViewWidth && option.getHeight() >= textureViewHeight) {
bigEnough.add(option);
} else {
notBigEnough.add(option);
}
}
}
// Pick the smallest of those big enough. If there is no one big enough, pick the
// largest of those not big enough.
if (bigEnough.size() > 0) {
return Collections.min(bigEnough, new CompareSizesByArea());
} else if (notBigEnough.size() > 0) {
return Collections.max(notBigEnough, new CompareSizesByArea());
} else {
return choices[0];
}
}
@Override
public void autoFocus() {
startPreview();
}
private void createImageReader() {
mImageReader = ImageReader.newInstance(mPreviewSize.getWidth(), mPreviewSize.getHeight(), ImageFormat.JPEG, 2);
}
@Override
public void closeCamera() {
if (mImageReader != null) {
mImageReader.close();
}
if (mCameraDevice != null) {
mCameraDevice.close();
}
if (mCameraCaptureSession != null) {
mCameraCaptureSession.close();
}
}
private static class CompareSizesByArea implements Comparator<Size> {
@Override
public int compare(Size lhs, Size rhs) {
return Long.signum((long) lhs.getWidth() * lhs.getHeight() - (long) rhs.getWidth() * rhs.getHeight());
}
}
@Override
public String getCameraId() {
try {
String[] idList = mCameraManager.getCameraIdList();
int currentFacing = facingFrontPreview() ? CameraCharacteristics.LENS_FACING_FRONT : CameraCharacteristics.LENS_FACING_BACK;
for (String id : idList) {
CameraCharacteristics characteristics = mCameraManager.getCameraCharacteristics(id);
int facing = characteristics.get(CameraCharacteristics.LENS_FACING);
if (facing != currentFacing) {
continue;
}
return id;
}
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
@Override
public void startPreview() {
if (mCameraCaptureSession != null) {
try {
mPreviewBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
mCameraCaptureSession.setRepeatingRequest(mPreviewBuilder.build(), mCaptureCallback, mCameraHandler);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
// Copyright 2018 Xiaomi, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.xiaomi.mace.demo.camera;
import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.SurfaceTexture;
import android.os.Handler;
import android.os.HandlerThread;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.TextureView.SurfaceTextureListener;
import android.view.View;
import com.xiaomi.mace.demo.AppModel;
import com.xiaomi.mace.demo.MaceApp;
import java.nio.FloatBuffer;
import static com.xiaomi.mace.demo.Constant.CAMERA_PERMISSION_REQ;
public abstract class CameraEngage implements SurfaceTextureListener {
/**
* preview height and width
*/
int mPreviewHeight;
int mPreviewWidth;
/**
* show camera view
*/
CameraTextureView mTextureView;
SurfaceTexture mSurfaceTexture;
/**
* switch camera use
*/
private boolean mFacingFront = false;
/**
* camera background thread
*/
private HandlerThread mBackgroundHandlerThread;
private Handler mBackgroundHandler;
/**
* mace need data size width and height
*/
private static final int FINAL_SIZE = 224;
/**
* storage rgb value
*/
private int[] colorValues;
/**
* mace float[] input
*/
private FloatBuffer floatBuffer;
private final Object lock = new Object();
private boolean isCapturePic = false;
private Runnable mHandleCapturePicRunnable = new Runnable() {
@Override
public void run() {
synchronized (lock) {
if (isCapturePic) {
handleCapturePic();
}
}
mBackgroundHandler.postDelayed(mHandleCapturePicRunnable, 200);
}
};
private void handleCapturePic() {
if (mTextureView != null) {
Bitmap bitmap = mTextureView.getBitmap(FINAL_SIZE, FINAL_SIZE);
if (bitmap != null) {
bitmap.getPixels(colorValues, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());
handleColorRgbs();
// EventBus.getDefault().post(new MessageEvent.PicEvent(bitmap));
bitmap.recycle();
}
}
}
public CameraEngage(CameraTextureView mTextureView) {
this.mTextureView = mTextureView;
colorValues = new int[FINAL_SIZE * FINAL_SIZE];
float[] floatValues = new float[FINAL_SIZE * FINAL_SIZE * 3];
floatBuffer = FloatBuffer.wrap(floatValues, 0, FINAL_SIZE * FINAL_SIZE * 3);
mPreviewHeight = MaceApp.app.getResources().getDisplayMetrics().heightPixels;
mPreviewWidth = MaceApp.app.getResources().getDisplayMetrics().widthPixels;
mTextureView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
autoFocus();
}
});
}
public void openCamera(int width, int height) {
startCapturePic();
}
public abstract void autoFocus();
public abstract void closeCamera();
public abstract String getCameraId();
public abstract void startPreview();
boolean facingFrontPreview() {
return mFacingFront;
}
public void setFacingFront(boolean facingFront) {
this.mFacingFront = facingFront;
onResume();
}
private void handleColorRgbs() {
floatBuffer.rewind();
for (int i = 0; i < colorValues.length; i++) {
int value = colorValues[i];
floatBuffer.put((((value >> 16) & 0xFF) - 128f)/ 128f);
floatBuffer.put((((value >> 8) & 0xFF) - 128f) / 128f);
floatBuffer.put(((value & 0xFF) - 128f) / 128f);
}
AppModel.instance.maceMobilenetClassify(floatBuffer.array());
}
public void onResume() {
if (mTextureView.isAvailable()) {
openCamera(mTextureView.getWidth(), mTextureView.getHeight());
} else {
mTextureView.setSurfaceTextureListener(this);
}
}
private void startCapturePic() {
mBackgroundHandlerThread = new HandlerThread("captureBackground");
mBackgroundHandlerThread.start();
mBackgroundHandler = new Handler(mBackgroundHandlerThread.getLooper());
synchronized (lock) {
isCapturePic = true;
}
mBackgroundHandler.post(mHandleCapturePicRunnable);
}
private void stopBackgroundThread() {
try {
mBackgroundHandlerThread.quitSafely();
mBackgroundHandlerThread.join();
mBackgroundHandler = null;
mBackgroundHandlerThread = null;
synchronized (lock) {
isCapturePic = false;
}
} catch (Exception e) {
Log.e(this.getClass().getName(), "stopBackgroundThread" + e);
}
}
public void onPause() {
closeCamera();
stopBackgroundThread();
}
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
mSurfaceTexture = surface;
openCamera(width, height);
}
@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
}
@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
return false;
}
@Override
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
}
boolean checkCameraPermission() {
if (ContextCompat.checkSelfPermission(mTextureView.getContext(), Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED ||
ContextCompat.checkSelfPermission(mTextureView.getContext(), Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
((Activity) mTextureView.getContext()).requestPermissions(new String[]{Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE}, CAMERA_PERMISSION_REQ);
return false;
}
return true;
}
}
// Copyright 2018 Xiaomi, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.xiaomi.mace.demo.camera;
import android.content.Context;
import android.util.AttributeSet;
import android.view.TextureView;
public class CameraTextureView extends TextureView {
private int ratioWidth;
private int ratioHeight;
public CameraTextureView(Context context) {
super(context);
}
public CameraTextureView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CameraTextureView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public void setRatio(int width, int height) {
this.ratioWidth = width;
this.ratioHeight = height;
requestLayout();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
if (ratioWidth <= 0 || ratioHeight <= 0) {
setMeasuredDimension(width, height);
} else {
setMeasuredDimension(width, width * ratioHeight / ratioWidth);
}
}
}
// Copyright 2018 Xiaomi, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.xiaomi.mace.demo.camera;
import android.app.Activity;
import android.app.DialogFragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import com.xiaomi.mace.demo.R;
import java.util.ArrayList;
import java.util.List;
public class ContextMenuDialog extends DialogFragment {
private List<String> items = new ArrayList<>();
private OnClickItemListener listener;
public static void show(Activity activity, List<String> items, OnClickItemListener listener) {
ContextMenuDialog dialog = new ContextMenuDialog();
dialog.items = items;
dialog.listener = listener;
dialog.show(activity.getFragmentManager(), "");
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View root = inflater.inflate(R.layout.layout_dialog, null);
final ListView listView = root.findViewById(R.id.list_menu);
ItemAdapter itemAdapter = new ItemAdapter();
listView.setAdapter(itemAdapter);
itemAdapter.setItems(items);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (listener != null) {
listener.onCLickItem(items.get(position));
}
dismissAllowingStateLoss();
}
});
return root;
}
public static interface OnClickItemListener {
void onCLickItem(String content);
}
}
// Copyright 2018 Xiaomi, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.xiaomi.mace.demo.camera;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.xiaomi.mace.demo.R;
import java.util.ArrayList;
import java.util.List;
public class ItemAdapter extends BaseAdapter {
private List<String> items = new ArrayList<>();
public void setItems(List<String> items) {
this.items = items;
notifyDataSetChanged();
}
@Override
public int getCount() {
return items.size();
}
@Override
public Object getItem(int position) {
return items.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder;
if (convertView != null && convertView.getTag() instanceof Holder) {
holder = (Holder) convertView.getTag();
} else {
convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_item, null);
holder = new Holder();
holder.itemView = convertView.findViewById(R.id.tv_item);
convertView.setTag(holder);
}
holder.itemView.setText(items.get(position));
return convertView;
}
class Holder {
TextView itemView;
}
}
// Copyright 2018 Xiaomi, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.xiaomi.mace.demo.camera;
import android.graphics.Bitmap;
import com.xiaomi.mace.demo.result.ResultData;
public class MessageEvent {
public static class MaceResultEvent {
ResultData data;
public MaceResultEvent(ResultData data) {
this.data = data;
}
public ResultData getData() {
return data;
}
}
public static class PicEvent {
Bitmap bitmap;
public PicEvent(Bitmap bitmap) {
this.bitmap = bitmap;
}
public Bitmap getBitmap() {
return bitmap;
}
}
public static class OutputSizeEvent {
public int width;
public int height;
public OutputSizeEvent(int width, int height) {
this.width = width;
this.height = height;
}
}
}
// Copyright 2018 Xiaomi, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.xiaomi.mace.demo.camera;
public interface MobilenetResultCallback {
void onMaceResult(float[] result, long costTime);
}
// Copyright 2018 Xiaomi, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.xiaomi.mace.demo.result;
import android.os.Environment;
import java.io.File;
public class InitData {
public static final String[] DEVICES = new String[]{"CPU", "GPU"};
public static final String[] MODELS = new String[]{"mobilenet_v1", "mobilenet_v2"};
private String model;
private String device = "";
private int ompNumThreads;
private int cpuAffinityPolicy;
private int gpuPerfHint;
private int gpuPriorityHint;
private String kernelPath = "";
public InitData() {
model = MODELS[0];
ompNumThreads = 2;
cpuAffinityPolicy = 1;
gpuPerfHint = 3;
gpuPriorityHint = 3;
device = DEVICES[0];
kernelPath = Environment.getDownloadCacheDirectory().getAbsolutePath() + File.separator + "mace";
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public String getDevice() {
return device;
}
public void setDevice(String device) {
this.device = device;
}
public int getOmpNumThreads() {
return ompNumThreads;
}
public void setOmpNumThreads(int ompNumThreads) {
this.ompNumThreads = ompNumThreads;
}
public int getCpuAffinityPolicy() {
return cpuAffinityPolicy;
}
public void setCpuAffinityPolicy(int cpuAffinityPolicy) {
this.cpuAffinityPolicy = cpuAffinityPolicy;
}
public int getGpuPerfHint() {
return gpuPerfHint;
}
public void setGpuPerfHint(int gpuPerfHint) {
this.gpuPerfHint = gpuPerfHint;
}
public int getGpuPriorityHint() {
return gpuPriorityHint;
}
public void setGpuPriorityHint(int gpuPriorityHint) {
this.gpuPriorityHint = gpuPriorityHint;
}
public String getKernelPath() {
return kernelPath;
}
public void setKernelPath(String kernelPath) {
this.kernelPath = kernelPath;
}
}
// Copyright 2018 Xiaomi, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.xiaomi.mace.demo.result;
import android.content.res.AssetManager;
import android.util.Log;
import com.xiaomi.mace.demo.MaceApp;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class LabelCache {
private static LabelCache labelCache;
private LabelCache() {
readCacheLabelFromLocalFile();
}
private List<Float> floatList = new ArrayList<>();
private List<String> resultLabel = new ArrayList<>();
private ResultData mResultData;
public static LabelCache instance() {
if (labelCache == null) {
synchronized (LabelCache.class) {
if (labelCache == null) {
labelCache = new LabelCache();
}
}
}
return labelCache;
}
private void readCacheLabelFromLocalFile() {
try {
AssetManager assetManager = MaceApp.app.getApplicationContext().getAssets();
BufferedReader reader = new BufferedReader(new InputStreamReader(assetManager.open("cacheLabel.txt")));
String readLine = null;
while ((readLine = reader.readLine()) != null) {
Log.d("labelCache", "readLine = " + readLine);
resultLabel.add(readLine);
}
reader.close();
} catch (Exception e) {
Log.e("labelCache", "error " + e);
}
}
public ResultData getResultFirst(float[] floats) {
floatList.clear();
for (float f : floats) {
floatList.add(f);
}
float maxResult = Collections.max(floatList);
int indexResult = floatList.indexOf(maxResult);
if (indexResult < resultLabel.size()) {
String result = resultLabel.get(indexResult);
if (result != null) {
if (mResultData == null) {
mResultData = new ResultData(result, maxResult);
} else {
mResultData.updateData(result, maxResult);
}
return mResultData;
}
}
return null;
}
}
// Copyright 2018 Xiaomi, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.xiaomi.mace.demo.result;
public class ResultData {
public String name;
public float probability;
public long costTime; //毫秒
public ResultData(String name, float probability) {
this.name = name;
this.probability = probability;
}
public ResultData(String name) {
this.name = name;
}
public void updateData(String name, float probability) {
this.name = name;
this.probability = probability;
}
}
<vector xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:aapt="http://schemas.android.com/aapt"
android:width="108dp"
android:height="108dp"
android:viewportHeight="108"
android:viewportWidth="108">
<path
android:fillType="evenOdd"
android:pathData="M32,64C32,64 38.39,52.99 44.13,50.95C51.37,48.37 70.14,49.57 70.14,49.57L108.26,87.69L108,109.01L75.97,107.97L32,64Z"
android:strokeColor="#00000000"
android:strokeWidth="1">
<aapt:attr name="android:fillColor">
<gradient
android:endX="78.5885"
android:endY="90.9159"
android:startX="48.7653"
android:startY="61.0927"
android:type="linear">
<item
android:color="#44000000"
android:offset="0.0" />
<item
android:color="#00000000"
android:offset="1.0" />
</gradient>
</aapt:attr>
</path>
<path
android:fillColor="#FFFFFF"
android:fillType="nonZero"
android:pathData="M66.94,46.02L66.94,46.02C72.44,50.07 76,56.61 76,64L32,64C32,56.61 35.56,50.11 40.98,46.06L36.18,41.19C35.45,40.45 35.45,39.3 36.18,38.56C36.91,37.81 38.05,37.81 38.78,38.56L44.25,44.05C47.18,42.57 50.48,41.71 54,41.71C57.48,41.71 60.78,42.57 63.68,44.05L69.11,38.56C69.84,37.81 70.98,37.81 71.71,38.56C72.44,39.3 72.44,40.45 71.71,41.19L66.94,46.02ZM62.94,56.92C64.08,56.92 65,56.01 65,54.88C65,53.76 64.08,52.85 62.94,52.85C61.8,52.85 60.88,53.76 60.88,54.88C60.88,56.01 61.8,56.92 62.94,56.92ZM45.06,56.92C46.2,56.92 47.13,56.01 47.13,54.88C47.13,53.76 46.2,52.85 45.06,52.85C43.92,52.85 43,53.76 43,54.88C43,56.01 43.92,56.92 45.06,56.92Z"
android:strokeColor="#00000000"
android:strokeWidth="1" />
</vector>
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="108dp"
android:height="108dp"
android:viewportHeight="108"
android:viewportWidth="108">
<path
android:fillColor="#26A69A"
android:pathData="M0,0h108v108h-108z" />
<path
android:fillColor="#00000000"
android:pathData="M9,0L9,108"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M19,0L19,108"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M29,0L29,108"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M39,0L39,108"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M49,0L49,108"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M59,0L59,108"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M69,0L69,108"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M79,0L79,108"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M89,0L89,108"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M99,0L99,108"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M0,9L108,9"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M0,19L108,19"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M0,29L108,29"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M0,39L108,39"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M0,49L108,49"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M0,59L108,59"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M0,69L108,69"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M0,79L108,79"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M0,89L108,89"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M0,99L108,99"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M19,29L89,29"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M19,39L89,39"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M19,49L89,49"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M19,59L89,59"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M19,69L89,69"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M19,79L89,79"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M29,19L29,89"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M39,19L39,89"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M49,19L49,89"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M59,19L59,89"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M69,19L69,89"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M79,19L79,89"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
</vector>
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000"
tools:context="com.xiaomi.mace.demo.CameraActivity">
<com.xiaomi.mace.demo.camera.CameraTextureView
android:id="@+id/camera_texture"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:id="@+id/tv_show_result"
android:layout_width="200dp"
android:layout_height="100dp"
android:layout_gravity="center_horizontal|bottom"
android:background="#fff"
android:paddingLeft="20dp"
android:gravity="center_vertical"
android:text="result none" />
<ImageView
android:id="@+id/iv_picture"
android:layout_width="200dp"
android:layout_height="200dp"
android:scaleType="center"
android:layout_gravity="right|bottom" />
<Button
android:id="@+id/tv_select_mode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="25dp"
android:text="模式选择" />
<Button
android:id="@+id/tv_select_phone_type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_margin="25dp"
android:text="手机配置选择"/>
</FrameLayout>
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/list_menu"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</FrameLayout>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tv_item"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:paddingBottom="10dp"
android:paddingLeft="50dp"
android:paddingRight="50dp"
android:paddingTop="10dp" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
<background android:drawable="@drawable/ic_launcher_background" />
<foreground android:drawable="@drawable/ic_launcher_foreground" />
</adaptive-icon>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
<background android:drawable="@drawable/ic_launcher_background" />
<foreground android:drawable="@drawable/ic_launcher_foreground" />
</adaptive-icon>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
</resources>
<resources>
<string name="app_name">识物</string>
</resources>
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
# Project-wide Gradle settings.
# IDE (e.g. Android Studio) users:
# Gradle settings configured through the IDE *will override*
# any settings specified in this file.
# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
org.gradle.jvmargs=-Xmx1536m
#android.useDeprecatedNdk=true
# When configured, Gradle will run in incubating parallel model.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
#Wed May 02 11:53:35 CST 2018
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip
#!/usr/bin/env bash
##############################################################################
##
## Gradle start up script for UN*X
##
##############################################################################
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS=""
APP_NAME="Gradle"
APP_BASE_NAME=`basename "$0"`
# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD="maximum"
warn ( ) {
echo "$*"
}
die ( ) {
echo
echo "$*"
echo
exit 1
}
# OS specific support (must be 'true' or 'false').
cygwin=false
msys=false
darwin=false
case "`uname`" in
CYGWIN* )
cygwin=true
;;
Darwin* )
darwin=true
;;
MINGW* )
msys=true
;;
esac
# Attempt to set APP_HOME
# Resolve links: $0 may be a link
PRG="$0"
# Need this for relative symlinks.
while [ -h "$PRG" ] ; do
ls=`ls -ld "$PRG"`
link=`expr "$ls" : '.*-> \(.*\)$'`
if expr "$link" : '/.*' > /dev/null; then
PRG="$link"
else
PRG=`dirname "$PRG"`"/$link"
fi
done
SAVED="`pwd`"
cd "`dirname \"$PRG\"`/" >/dev/null
APP_HOME="`pwd -P`"
cd "$SAVED" >/dev/null
CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
# Determine the Java command to use to start the JVM.
if [ -n "$JAVA_HOME" ] ; then
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
# IBM's JDK on AIX uses strange locations for the executables
JAVACMD="$JAVA_HOME/jre/sh/java"
else
JAVACMD="$JAVA_HOME/bin/java"
fi
if [ ! -x "$JAVACMD" ] ; then
die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi
else
JAVACMD="java"
which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi
# Increase the maximum file descriptors if we can.
if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
MAX_FD_LIMIT=`ulimit -H -n`
if [ $? -eq 0 ] ; then
if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
MAX_FD="$MAX_FD_LIMIT"
fi
ulimit -n $MAX_FD
if [ $? -ne 0 ] ; then
warn "Could not set maximum file descriptor limit: $MAX_FD"
fi
else
warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
fi
fi
# For Darwin, add options to specify how the application appears in the dock
if $darwin; then
GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
fi
# For Cygwin, switch paths to Windows format before running java
if $cygwin ; then
APP_HOME=`cygpath --path --mixed "$APP_HOME"`
CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
JAVACMD=`cygpath --unix "$JAVACMD"`
# We build the pattern for arguments to be converted via cygpath
ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
SEP=""
for dir in $ROOTDIRSRAW ; do
ROOTDIRS="$ROOTDIRS$SEP$dir"
SEP="|"
done
OURCYGPATTERN="(^($ROOTDIRS))"
# Add a user-defined pattern to the cygpath arguments
if [ "$GRADLE_CYGPATTERN" != "" ] ; then
OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
fi
# Now convert the arguments - kludge to limit ourselves to /bin/sh
i=0
for arg in "$@" ; do
CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
else
eval `echo args$i`="\"$arg\""
fi
i=$((i+1))
done
case $i in
(0) set -- ;;
(1) set -- "$args0" ;;
(2) set -- "$args0" "$args1" ;;
(3) set -- "$args0" "$args1" "$args2" ;;
(4) set -- "$args0" "$args1" "$args2" "$args3" ;;
(5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
(6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
(7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
(8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
(9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
esac
fi
# Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
function splitJvmOpts() {
JVM_OPTS=("$@")
}
eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
@if "%DEBUG%" == "" @echo off
@rem ##########################################################################
@rem
@rem Gradle startup script for Windows
@rem
@rem ##########################################################################
@rem Set local scope for the variables with windows NT shell
if "%OS%"=="Windows_NT" setlocal
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
set DEFAULT_JVM_OPTS=
set DIRNAME=%~dp0
if "%DIRNAME%" == "" set DIRNAME=.
set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%
@rem Find java.exe
if defined JAVA_HOME goto findJavaFromJavaHome
set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if "%ERRORLEVEL%" == "0" goto init
echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
goto fail
:findJavaFromJavaHome
set JAVA_HOME=%JAVA_HOME:"=%
set JAVA_EXE=%JAVA_HOME%/bin/java.exe
if exist "%JAVA_EXE%" goto init
echo.
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
goto fail
:init
@rem Get command-line arguments, handling Windowz variants
if not "%OS%" == "Windows_NT" goto win9xME_args
if "%@eval[2+2]" == "4" goto 4NT_args
:win9xME_args
@rem Slurp the command line arguments.
set CMD_LINE_ARGS=
set _SKIP=2
:win9xME_args_slurp
if "x%~1" == "x" goto execute
set CMD_LINE_ARGS=%*
goto execute
:4NT_args
@rem Get arguments from the 4NT Shell from JP Software
set CMD_LINE_ARGS=%$
:execute
@rem Setup the command line
set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
@rem Execute Gradle
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
:end
@rem End local scope for the variables with windows NT shell
if "%ERRORLEVEL%"=="0" goto mainEnd
:fail
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
rem the _cmd.exe /c_ return code!
if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
exit /b 1
:mainEnd
if "%OS%"=="Windows_NT" endlocal
:omega
# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html
# Sets the minimum version of CMake required to build the native library.
cmake_minimum_required(VERSION 3.4.1)
# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.
#set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/../app/libs/${ANDROID_ABI})
include_directories(${CMAKE_SOURCE_DIR}/)
file(GLOB static_file ${CMAKE_SOURCE_DIR}/../../../build/demo_app_models/lib/arm64-v8a/*.a)
MESSAGE(STATUS "FILE URL = ${CMAKE_SOURCE_DIR}")
MESSAGE(STATUS "FILE URL = ${static_file}")
foreach(fileStr ${static_file})
set(tmpstr ${fileStr})
MESSAGE(STATUS "FILE URL = ${tmpstr}")
endforeach()
add_library (mace_mobile_lib STATIC IMPORTED)
set_target_properties(mace_mobile_lib PROPERTIES IMPORTED_LOCATION ${tmpstr})
add_library( # Sets the name of the library.
mace_mobile_jni
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
src/main/cpp/image_classify.cc )
# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.
find_library( # Sets the name of the path variable.
log-lib
# Specifies the name of the NDK library that
# you want CMake to locate.
log )
# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.
target_link_libraries( # Specifies the target library.
mace_mobile_jni
mace_mobile_lib
# Links the target library to the log library
# included in the NDK.
${log-lib} )
\ No newline at end of file
apply plugin: 'com.android.library'
android {
compileSdkVersion 26
compileOptions {
}
defaultConfig {
minSdkVersion 23
targetSdkVersion 26
versionCode 1
versionName "1.0"
externalNativeBuild {
cmake {
cppFlags "-std=c++14 -fopenmp"
abiFilters "arm64-v8a"
}
}
}
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets {
main {
jniLibs.srcDirs = ["src/main/jniLibs"]
jni.srcDirs = ['src/cpp']
}
}
task ndkClean(type: Delete) {
delete fileTree('.externalNativeBuild') {
exclude defaultConfig.ndk.abiFilters.collect { '**/' + it }
}
}
tasks.findByPath(':clean').dependsOn ndkClean
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
}
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable
# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xiaomi.mace" />
// Copyright 2018 Xiaomi, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "src/main/cpp/image_classify.h"
#include <android/log.h>
#include <jni.h>
#include <algorithm>
#include <functional>
#include <map>
#include <memory>
#include <string>
#include <vector>
#include <numeric>
#include "src/main/cpp/include/mace.h"
#include "src/main/cpp/include/mace_runtime.h"
#include "src/main/cpp/include/mace_engine_factory.h"
namespace {
struct ModelInfo {
std::string input_name;
std::string output_name;
std::vector<int64_t> input_shape;
std::vector<int64_t> output_shape;
};
struct MaceContext {
std::shared_ptr<mace::MaceEngine> engine;
std::shared_ptr<mace::KVStorageFactory> storage_factory;
std::string model_name;
mace::DeviceType device_type = mace::DeviceType::CPU;
std::map<std::string, ModelInfo> model_infos = {
{"mobilenet_v1", {"input", "MobilenetV1/Predictions/Reshape_1",
{1, 224, 224, 3}, {1, 1, 1, 1001}}},
{"mobilenet_v2", {"input", "MobilenetV2/Predictions/Reshape_1",
{1, 224, 224, 3}, {1, 1, 1, 1001}}}
};
};
mace::DeviceType ParseDeviceType(const std::string &device) {
if (device.compare("CPU") == 0) {
return mace::DeviceType::CPU;
} else if (device.compare("GPU") == 0) {
return mace::DeviceType::GPU;
} else if (device.compare("HEXAGON") == 0) {
return mace::DeviceType::HEXAGON;
} else {
return mace::DeviceType::CPU;
}
}
MaceContext& GetMaceContext() {
// stay for the app's life time, only initialize once
static auto *mace_context = new MaceContext;
return *mace_context;
}
} // namespace
JNIEXPORT jint JNICALL Java_com_xiaomi_mace_JniMaceUtils_maceMobilenetSetAttrs(
JNIEnv *env, jclass thisObj, jint omp_num_threads, jint cpu_affinity_policy,
jint gpu_perf_hint, jint gpu_priority_hint, jstring kernel_path) {
MaceContext &mace_context = GetMaceContext();
mace::MaceStatus status;
// openmp
status = mace::SetOpenMPThreadPolicy(
omp_num_threads,
static_cast<mace::CPUAffinityPolicy>(cpu_affinity_policy));
__android_log_print(ANDROID_LOG_ERROR,
"image_classify attrs",
"openmp result: %d, threads: %d, cpu: %d",
status, omp_num_threads, cpu_affinity_policy);
// gpu
mace::SetGPUHints(
static_cast<mace::GPUPerfHint>(gpu_perf_hint),
static_cast<mace::GPUPriorityHint>(gpu_priority_hint));
__android_log_print(ANDROID_LOG_ERROR,
"image_classify attrs",
"gpu perf: %d, priority: %d",
gpu_perf_hint, gpu_priority_hint);
// opencl cache
const char *kernel_path_ptr = env->GetStringUTFChars(kernel_path, nullptr);
if (kernel_path_ptr == nullptr) return JNI_ERR;
const std::string kernel_file_path(kernel_path_ptr);
mace_context.storage_factory.reset(
new mace::FileStorageFactory(kernel_file_path));
mace::SetKVStorageFactory(mace_context.storage_factory);
env->ReleaseStringUTFChars(kernel_path, kernel_path_ptr);
return JNI_OK;
}
JNIEXPORT jint JNICALL
Java_com_xiaomi_mace_JniMaceUtils_maceMobilenetCreateEngine(
JNIEnv *env, jclass thisObj, jstring model_name_str, jstring device) {
MaceContext &mace_context = GetMaceContext();
// parse model name
const char *model_name_ptr = env->GetStringUTFChars(model_name_str, nullptr);
if (model_name_ptr == nullptr) return JNI_ERR;
mace_context.model_name.assign(model_name_ptr);
env->ReleaseStringUTFChars(model_name_str, model_name_ptr);
// load model input and output name
auto model_info_iter =
mace_context.model_infos.find(mace_context.model_name);
if (model_info_iter == mace_context.model_infos.end()) {
__android_log_print(ANDROID_LOG_ERROR,
"image_classify",
"Invalid model name: %s",
mace_context.model_name.c_str());
return JNI_ERR;
}
std::vector<std::string> input_names = {model_info_iter->second.input_name};
std::vector<std::string> output_names = {model_info_iter->second.output_name};
// get device
const char *device_ptr = env->GetStringUTFChars(device, nullptr);
if (device_ptr == nullptr) return JNI_ERR;
mace_context.device_type = ParseDeviceType(device_ptr);
env->ReleaseStringUTFChars(device, device_ptr);
__android_log_print(ANDROID_LOG_ERROR,
"image_classify attrs",
"device: %d",
mace_context.device_type);
mace::MaceStatus create_engine_status =
CreateMaceEngineFromCode(mace_context.model_name,
std::string(),
input_names,
output_names,
mace_context.device_type,
&mace_context.engine);
__android_log_print(ANDROID_LOG_ERROR,
"image_classify attrs",
"create result: %d",
create_engine_status);
return create_engine_status == mace::MaceStatus::MACE_SUCCESS ?
JNI_OK : JNI_ERR;
}
JNIEXPORT jfloatArray JNICALL
Java_com_xiaomi_mace_JniMaceUtils_maceMobilenetClassify(
JNIEnv *env, jclass thisObj, jfloatArray input_data) {
MaceContext &mace_context = GetMaceContext();
// prepare input and output
auto model_info_iter =
mace_context.model_infos.find(mace_context.model_name);
if (model_info_iter == mace_context.model_infos.end()) {
__android_log_print(ANDROID_LOG_ERROR,
"image_classify",
"Invalid model name: %s",
mace_context.model_name.c_str());
return nullptr;
}
const ModelInfo &model_info = model_info_iter->second;
const std::string &input_name = model_info.input_name;
const std::string &output_name = model_info.output_name;
const std::vector<int64_t> &input_shape = model_info.input_shape;
const std::vector<int64_t> &output_shape = model_info.output_shape;
const int64_t input_size =
std::accumulate(input_shape.begin(), input_shape.end(), 1,
std::multiplies<int64_t>());
const int64_t output_size =
std::accumulate(output_shape.begin(), output_shape.end(), 1,
std::multiplies<int64_t>());
// load input
jfloat *input_data_ptr = env->GetFloatArrayElements(input_data, nullptr);
if (input_data_ptr == nullptr) return nullptr;
jsize length = env->GetArrayLength(input_data);
if (length != input_size) return nullptr;
std::map<std::string, mace::MaceTensor> inputs;
std::map<std::string, mace::MaceTensor> outputs;
// construct input
auto buffer_in = std::shared_ptr<float>(new float[input_size],
std::default_delete<float[]>());
std::copy_n(input_data_ptr, input_size, buffer_in.get());
env->ReleaseFloatArrayElements(input_data, input_data_ptr, 0);
inputs[input_name] = mace::MaceTensor(input_shape, buffer_in);
// construct output
auto buffer_out = std::shared_ptr<float>(new float[output_size],
std::default_delete<float[]>());
outputs[output_name] = mace::MaceTensor(output_shape, buffer_out);
// run model
mace_context.engine->Run(inputs, &outputs);
// transform output
jfloatArray jOutputData = env->NewFloatArray(output_size); // allocate
if (jOutputData == nullptr) return nullptr;
env->SetFloatArrayRegion(jOutputData, 0, output_size,
outputs[output_name].data().get()); // copy
return jOutputData;
}
// Copyright 2018 Xiaomi, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/* Header for class com_xiaomi_mace_JniMaceUtils */
#ifndef MACE_EXAMPLES_ANDROID_MACELIBRARY_SRC_MAIN_CPP_IMAGE_CLASSIFY_H_
#define MACE_EXAMPLES_ANDROID_MACELIBRARY_SRC_MAIN_CPP_IMAGE_CLASSIFY_H_
#include <jni.h>
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_xiaomi_mace_JniMaceUtils
* Method: maceMobilenetSetAttrs
* Signature: (Ljava/lang/String;IIIILjava/lang/String;)I
*/
JNIEXPORT jint JNICALL Java_com_xiaomi_mace_JniMaceUtils_maceMobilenetSetAttrs
(JNIEnv *, jclass, jint, jint, jint, jint, jstring);
/*
* Class: com_xiaomi_mace_JniMaceUtils
* Method: maceMobilenetCreateEngine
* Signature: (Ljava/lang/String;)I
*/
JNIEXPORT jint JNICALL
Java_com_xiaomi_mace_JniMaceUtils_maceMobilenetCreateEngine
(JNIEnv *, jclass, jstring, jstring);
/*
* Class: com_xiaomi_mace_JniMaceUtils
* Method: maceMobilenetClassify
* Signature: ([F)[F
*/
JNIEXPORT jfloatArray JNICALL
Java_com_xiaomi_mace_JniMaceUtils_maceMobilenetClassify
(JNIEnv *, jclass, jfloatArray);
#ifdef __cplusplus
}
#endif
#endif // MACE_EXAMPLES_ANDROID_MACELIBRARY_SRC_MAIN_CPP_IMAGE_CLASSIFY_H_
// Copyright 2018 Xiaomi, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.xiaomi.mace;
public class JniMaceUtils {
static {
System.loadLibrary("mace_mobile_jni");
}
public static native int maceMobilenetSetAttrs(int ompNumThreads, int cpuAffinityPolicy, int gpuPerfHint, int gpuPriorityHint, String kernelPath);
public static native int maceMobilenetCreateEngine(String model, String device);
public static native float[] maceMobilenetClassify(float[] input);
}
<resources>
<string name="app_name">MaceLibrary</string>
</resources>
VERS_1.0 {
# Export JNI symbols.
global:
Java_*;
JNI_OnLoad;
JNI_OnUnload;
# Hide everything else.
local:
*;
};
include ':app', ':macelibrary'
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册