@@ -12,30 +12,13 @@ import {
1212 Skia ,
1313 AlphaType ,
1414 ColorType ,
15+ SkImage ,
1516} from '@shopify/react-native-skia' ;
1617import { View , StyleSheet , Image } from 'react-native' ;
1718import React , { useContext , useEffect , useState } from 'react' ;
1819import { GeneratingContext } from '../../context' ;
1920import ScreenWrapper from '../../ScreenWrapper' ;
2021
21- const width = 224 ;
22- const height = 224 ;
23-
24- let pixels = new Uint8Array ( width * height * 4 ) ;
25- pixels . fill ( 255 ) ;
26-
27- let data = Skia . Data . fromBytes ( pixels ) ;
28- let img = Skia . Image . MakeImage (
29- {
30- width : width ,
31- height : height ,
32- alphaType : AlphaType . Opaque ,
33- colorType : ColorType . RGBA_8888 ,
34- } ,
35- data ,
36- width * 4
37- ) ;
38-
3922const numberToColor : number [ ] [ ] = [
4023 [ 255 , 87 , 51 ] , // 0 Red
4124 [ 51 , 255 , 87 ] , // 1 Green
@@ -67,48 +50,58 @@ export default function ImageSegmentationScreen() {
6750 setGlobalGenerating ( model . isGenerating ) ;
6851 } , [ model . isGenerating , setGlobalGenerating ] ) ;
6952 const [ imageUri , setImageUri ] = useState ( '' ) ;
53+ const [ imageSize , setImageSize ] = useState ( { width : 0 , height : 0 } ) ;
54+ const [ segImage , setSegImage ] = useState < SkImage | null > ( null ) ;
55+ const [ canvasSize , setCanvasSize ] = useState ( { width : 0 , height : 0 } ) ;
7056
7157 const handleCameraPress = async ( isCamera : boolean ) => {
7258 const image = await getImage ( isCamera ) ;
73- const uri = image ?. uri ;
74- setImageUri ( uri as string ) ;
59+ if ( ! image ?. uri ) return ;
60+ setImageUri ( image . uri ) ;
61+ setImageSize ( {
62+ width : image . width ?? 0 ,
63+ height : image . height ?? 0 ,
64+ } ) ;
65+ setSegImage ( null ) ;
7566 } ;
7667
77- const [ resultPresent , setResultPresent ] = useState ( false ) ;
78-
7968 const runForward = async ( ) => {
80- if ( imageUri ) {
81- try {
82- const output = await model . forward ( imageUri ) ;
83- pixels = new Uint8Array ( width * height * 4 ) ;
69+ if ( ! imageUri || imageSize . width === 0 || imageSize . height === 0 ) return ;
70+ try {
71+ const { width, height } = imageSize ;
72+ const output = await model . forward ( imageUri , [ DeeplabLabel . ARGMAX ] ) ;
73+ const argmax = output [ DeeplabLabel . ARGMAX ] || [ ] ;
74+ const uniqueValues = new Set < number > ( ) ;
75+ for ( let i = 0 ; i < argmax . length ; i ++ ) {
76+ uniqueValues . add ( argmax [ i ] ) ;
77+ }
78+ const pixels = new Uint8Array ( width * height * 4 ) ;
8479
85- for ( let x = 0 ; x < width ; x ++ ) {
86- for ( let y = 0 ; y < height ; y ++ ) {
87- for ( let i = 0 ; i < 3 ; i ++ ) {
88- pixels [ ( x * height + y ) * 4 + i ] =
89- numberToColor [
90- ( output [ DeeplabLabel . ARGMAX ] || [ ] ) [ x * height + y ]
91- ] [ i ] ;
92- }
93- pixels [ ( x * height + y ) * 4 + 3 ] = 255 ;
94- }
80+ for ( let row = 0 ; row < height ; row ++ ) {
81+ for ( let col = 0 ; col < width ; col ++ ) {
82+ const idx = row * width + col ;
83+ const color = numberToColor [ argmax [ idx ] ] || [ 0 , 0 , 0 ] ;
84+ pixels [ idx * 4 ] = color [ 0 ] ;
85+ pixels [ idx * 4 + 1 ] = color [ 1 ] ;
86+ pixels [ idx * 4 + 2 ] = color [ 2 ] ;
87+ pixels [ idx * 4 + 3 ] = 255 ;
9588 }
96-
97- data = Skia . Data . fromBytes ( pixels ) ;
98- img = Skia . Image . MakeImage (
99- {
100- width : width ,
101- height : height ,
102- alphaType : AlphaType . Opaque ,
103- colorType : ColorType . RGBA_8888 ,
104- } ,
105- data ,
106- width * 4
107- ) ;
108- setResultPresent ( true ) ;
109- } catch ( e ) {
110- console . error ( e ) ;
11189 }
90+
91+ const data = Skia . Data . fromBytes ( pixels ) ;
92+ const img = Skia . Image . MakeImage (
93+ {
94+ width,
95+ height,
96+ alphaType : AlphaType . Opaque ,
97+ colorType : ColorType . RGBA_8888 ,
98+ } ,
99+ data ,
100+ width * 4
101+ ) ;
102+ setSegImage ( img ) ;
103+ } catch ( e ) {
104+ console . error ( e ) ;
112105 }
113106 } ;
114107
@@ -135,16 +128,24 @@ export default function ImageSegmentationScreen() {
135128 }
136129 />
137130 </ View >
138- { resultPresent && (
139- < View style = { styles . canvasContainer } >
131+ { segImage && (
132+ < View
133+ style = { styles . canvasContainer }
134+ onLayout = { ( e ) =>
135+ setCanvasSize ( {
136+ width : e . nativeEvent . layout . width ,
137+ height : e . nativeEvent . layout . height ,
138+ } )
139+ }
140+ >
140141 < Canvas style = { styles . canvas } >
141142 < SkiaImage
142- image = { img }
143+ image = { segImage }
143144 fit = "contain"
144145 x = { 0 }
145146 y = { 0 }
146- width = { width }
147- height = { height }
147+ width = { canvasSize . width }
148+ height = { canvasSize . height }
148149 />
149150 </ Canvas >
150151 </ View >
@@ -181,7 +182,7 @@ const styles = StyleSheet.create({
181182 padding : 4 ,
182183 } ,
183184 canvas : {
184- width : width ,
185- height : height ,
185+ width : '100%' ,
186+ height : '100%' ,
186187 } ,
187188} ) ;
0 commit comments