1
1
import { useEffect } from 'react'
2
2
3
+ const SCORE_DIGITS = 4
4
+
5
+ const getLabelText = ( prediction ) => {
6
+ const scoreText = prediction . score . toFixed ( SCORE_DIGITS )
7
+ return prediction . class + ', score: ' + scoreText
8
+ }
9
+
3
10
const renderPredictions = ( predictions , canvasRef ) => {
4
11
const ctx = canvasRef . current . getContext ( '2d' )
5
12
ctx . clearRect ( 0 , 0 , ctx . canvas . width , ctx . canvas . height )
@@ -18,7 +25,7 @@ const renderPredictions = (predictions, canvasRef) => {
18
25
ctx . strokeRect ( x , y , width , height )
19
26
// Draw the label background.
20
27
ctx . fillStyle = '#00FFFF'
21
- const textWidth = ctx . measureText ( prediction . class ) . width
28
+ const textWidth = ctx . measureText ( getLabelText ( prediction ) ) . width
22
29
const textHeight = parseInt ( font , 10 ) // base 10
23
30
ctx . fillRect ( x , y , textWidth + 4 , textHeight + 4 )
24
31
} )
@@ -28,7 +35,7 @@ const renderPredictions = (predictions, canvasRef) => {
28
35
const y = prediction . bbox [ 1 ]
29
36
// Draw the text last to ensure it's on top.
30
37
ctx . fillStyle = '#000000'
31
- ctx . fillText ( prediction . class , x , y )
38
+ ctx . fillText ( getLabelText ( prediction ) , x , y )
32
39
} )
33
40
}
34
41
0 commit comments