Skip to content

Commit 7a58ad6

Browse files
authored
fix: Merge pull request #86 from UniversalDataTool/fix/duplicate-images
Support for duplicate images by refactoring indexing scheme
2 parents 006ae7e + 09075d0 commit 7a58ad6

File tree

6 files changed

+28
-21
lines changed

6 files changed

+28
-21
lines changed

src/Annotator/index.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ type Props = {
3131
enabledTools?: Array<string>,
3232
selectedTool?: String,
3333
showTags?: boolean,
34-
selectedImage?: string,
34+
selectedImage?: string | number,
3535
images?: Array<Image>,
3636
showPointDistances?: boolean,
3737
pointDistancePrecision?: number,
@@ -50,7 +50,7 @@ type Props = {
5050
export const Annotator = ({
5151
images,
5252
allowedArea,
53-
selectedImage = images && images.length > 0 ? images[0].src : undefined,
53+
selectedImage = images && images.length > 0 ? 0 : undefined,
5454
showPointDistances,
5555
pointDistancePrecision,
5656
showTags = true,
@@ -79,6 +79,10 @@ export const Annotator = ({
7979
onPrevImage,
8080
autoSegmentationOptions = { type: "autoseg" },
8181
}: Props) => {
82+
if (typeof selectedImage === "string") {
83+
selectedImage = (images || []).findIndex((img) => img.src === selectedImage)
84+
if (selectedImage === -1) selectedImage = undefined
85+
}
8286
const annotationType = images ? "image" : "video"
8387
const [state, dispatchToReducer] = useReducer(
8488
historyHandler(
@@ -143,10 +147,11 @@ export const Annotator = ({
143147
})
144148

145149
useEffect(() => {
146-
if (!selectedImage) return
150+
if (selectedImage === undefined) return
147151
dispatchToReducer({
148152
type: "SELECT_IMAGE",
149-
image: state.images.find((img) => img.src === selectedImage),
153+
imageIndex: selectedImage,
154+
image: state.images[selectedImage],
150155
})
151156
}, [selectedImage])
152157

src/Annotator/reducers/general-reducer.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,10 @@ export default (state: MainLayoutState, action: Action) => {
103103
)
104104
}
105105

106-
const setNewImage = (img: string | Object) => {
106+
const setNewImage = (img: string | Object, index: number) => {
107107
let { src, frameTime } = typeof img === "object" ? img : { src: img }
108108
return setIn(
109-
setIn(state, ["selectedImage"], src),
109+
setIn(state, ["selectedImage"], index),
110110
["selectedImageFrameTime"],
111111
frameTime
112112
)
@@ -117,7 +117,7 @@ export default (state: MainLayoutState, action: Action) => {
117117
return state
118118
}
119119
case "SELECT_IMAGE": {
120-
return setNewImage(action.image)
120+
return setNewImage(action.image, action.imageIndex)
121121
}
122122
case "CHANGE_REGION": {
123123
const regionIndex = getRegionIndex(action.region)
@@ -675,18 +675,27 @@ export default (state: MainLayoutState, action: Action) => {
675675
case "prev": {
676676
if (currentImageIndex === null) return state
677677
if (currentImageIndex === 0) return state
678-
return setNewImage(state.images[currentImageIndex - 1])
678+
return setNewImage(
679+
state.images[currentImageIndex - 1],
680+
currentImageIndex - 1
681+
)
679682
}
680683
case "next": {
681684
if (currentImageIndex === null) return state
682685
if (currentImageIndex === state.images.length - 1) return state
683-
return setNewImage(state.images[currentImageIndex + 1])
686+
return setNewImage(
687+
state.images[currentImageIndex + 1],
688+
currentImageIndex + 1
689+
)
684690
}
685691
case "clone": {
686692
if (currentImageIndex === null) return state
687693
if (currentImageIndex === state.images.length - 1) return state
688694
return setIn(
689-
setNewImage(state.images[currentImageIndex + 1]),
695+
setNewImage(
696+
state.images[currentImageIndex + 1],
697+
currentImageIndex + 1
698+
),
690699
["images", currentImageIndex + 1, "regions"],
691700
activeImage.regions
692701
)

src/Annotator/reducers/get-active-image.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,7 @@ export default (state) => {
55
pathToActiveImage,
66
activeImage
77
if (state.annotationType === "image") {
8-
currentImageIndex = state.images.findIndex(
9-
(img) =>
10-
img.src === state.selectedImage &&
11-
(state.selectedImageFrameTime === undefined ||
12-
img.frameTime === state.selectedImageFrameTime)
13-
)
8+
currentImageIndex = state.selectedImage
149
if (currentImageIndex === -1) {
1510
currentImageIndex = null
1611
activeImage = null

src/DebugSidebarBox/index.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ import React from "react"
44
import SidebarBoxContainer from "../SidebarBoxContainer"
55

66
export const DebugSidebarBox = ({ state, lastAction }: any) => {
7-
const image = (state.images || []).find(
8-
(img) => img.src === state.selectedImage
9-
)
7+
const image = (state.images || [])[state.selectedImage]
108
const region = image
119
? (image.regions || []).filter((r) => r.highlighted)
1210
: null

src/MainLayout/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ export const MainLayout = ({
125125
}
126126
realSize={activeImage ? activeImage.realSize : undefined}
127127
videoPlaying={state.videoPlaying}
128-
imageSrc={state.annotationType === "image" ? state.selectedImage : null}
128+
imageSrc={state.annotationType === "image" ? activeImage.src : null}
129129
videoSrc={state.annotationType === "video" ? state.videoSrc : null}
130130
pointDistancePrecision={state.pointDistancePrecision}
131131
createWithPrimary={state.selectedTool.includes("create")}

src/MainLayout/types.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ export type MainLayoutState =
101101

102102
export type Action =
103103
| {| type: "@@INIT" |}
104-
| {| type: "SELECT_IMAGE", image: Image |}
104+
| {| type: "SELECT_IMAGE", image: Image, imageIndex: number |}
105105
| {|
106106
type: "IMAGE_OR_VIDEO_LOADED",
107107
metadata: {

0 commit comments

Comments
 (0)