Skip to content

Commit 427710c

Browse files
authored
Merge pull request #48 from danforthcenter/napari-points-sam
Napari points sam
2 parents f9aa114 + d302091 commit 427710c

4 files changed

Lines changed: 64 additions & 23 deletions

File tree

docs/napari_naive_bayes_colors.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ to get data for naive bayes functions. Collect pixel training data in Napari, ra
99

1010
- **Parameters:**
1111
- img - RGB image to extract color information from
12-
- maskdict - dictionary of masks, output of [`napari_points_mask`](docs/napari_points_mask.md) for example
12+
- maskdict - dictionary of masks, output of [`napari_points_mask`](napari_points_mask.md) for example
1313
- filename - filename to save data, formatted to work with [Naive Bayes segmentation](https://plantcv.readthedocs.io/en/latest/tutorials/machine_learning_tutorial/)
1414

1515
- **Context:**

docs/napari_read_coor.md

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
## Read point data into Napari Format
22

3-
Save Points Labeled in Napari to a File
3+
Read points from a file or dictionary into Napari format
44

55
**plantcv.napari_read_coor**(*coor, dataformat = 'yx'*)
66

77
**returns** dictionary of points labeled by class
88

99
- **Parameters:**
1010
- coor - dictionary object of coordinates, or a path to json datafile with dictionary of point coordinates
11-
- dataformat - either 'yx' or 'xy', Napari takes data as y,x format. If data is 'xy' data is converted from x,y to y,x
11+
- dataformat - either 'yx', 'xy', or 'sam', Napari takes data as y,x format. If data is 'xy' data is converted from x,y to y,x.
12+
If data is 'sam' point data is formatted for input into ultralytics sam3 functions. If 'sam' format is selected the function does expect a dictionary with 'pos' and 'neg' points as labelled classes.
1213

1314
- **Context:**
14-
- Import previously labeled points, or points from other functions (e.g. [`pcvan.napari_read_coor`](napari_read_coor.md))
15+
- Import previously labeled points, or points from other functions (e.g. [`pcvan.napari_save_coor`](napari_save_coor.md))
1516

1617
- **Example use:**
1718
- Below
@@ -27,4 +28,18 @@ data = pcvan.napari_read_coor(coor ='coor.json', dataformat = 'xy')
2728

2829
```
2930

31+
- **Example use for training Segment Anything Model:**
32+
- Below
33+
34+
```python
35+
from ultralytics import SAM
36+
37+
model = SAM("sam3.pt")
38+
results = model.predict(source="./Example_image.jpg",
39+
points=data["points"],
40+
labels=data["labels"])
41+
results[0].show()
42+
43+
```
44+
3045
**Source Code:** [Here](https://github.com/danforthcenter/plantcv-annotate/blob/main/plantcv/annotate/napari_read_coor.py)

plantcv/annotate/napari_read_coor.py

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,21 @@
44

55

66
def napari_read_coor(coor, dataformat='yx'):
7-
"""
8-
open img in napari and label classes
9-
10-
Inputs:
11-
coor = either a dictionary of data or a path to a json file
12-
with dictionary of point coordinates
13-
dataformat = either 'yx' or 'xy'. Output of points function is in
14-
x,y format and Napari is in y,x format.
15-
16-
Returns:
17-
data = dictionary of data
18-
19-
:param coor: dict or str
20-
:param dataformat: str
21-
:return data: dictionary of data in y,x format for napari
22-
7+
"""Open img in napari and label classes
8+
9+
Parameters
10+
----------
11+
coor : dict or str
12+
Either a dictionary or path to json file of points and label classes.
13+
dataformat : str
14+
Use 'xy' for points function outputs, 'yx' for Napari outputs, and 'sam' for
15+
Segment Anything Model, which includes "pos" and "neg" labeled classes;
16+
defaults to 'yx'.
17+
18+
Returns
19+
----------
20+
dict
21+
Dictionary of points data.
2322
"""
2423
if isinstance(coor, dict):
2524
data = coor
@@ -34,4 +33,22 @@ def napari_read_coor(coor, dataformat='yx'):
3433
data1.update({key: data2})
3534
data = data1
3635

36+
if dataformat == 'sam':
37+
pointslist = []
38+
pointslabel = []
39+
40+
for i in enumerate(data['pos']):
41+
x, y = i[1]
42+
pointslist.append([x, y])
43+
pointslabel.append(1)
44+
45+
for i in enumerate(data['neg']):
46+
x, y = i[1]
47+
pointslist.append([x, y])
48+
pointslabel.append(0)
49+
50+
data1['points'] = [pointslist]
51+
data1['labels'] = [pointslabel]
52+
data = data1
53+
3754
return data

tests/test_napari_read_coor.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
def test_napari_read_coor_napari(test_data):
55
"""Test for PlantCV.Annotate"""
66
# Read in test data
7-
data = napari_read_coor(test_data.coor_data, 'yx')
7+
data = napari_read_coor(test_data.coor_data, dataformat='yx')
88

99
assert isinstance(data, dict)
1010

1111

1212
def test_napari_read_coor_other(test_data):
1313
"""Test for PlantCV.Annotate"""
1414
# Read in test data
15-
data = napari_read_coor(test_data.coor_data, 'xy')
15+
data = napari_read_coor(test_data.coor_data, dataformat='xy')
1616

1717
assert data['germinated'][0] == (10, 25)
1818

@@ -21,6 +21,15 @@ def test_napari_read_coor_flip():
2121
"""Test for PlantCV.Annotate"""
2222
# Read in test data
2323
coor = {"germinated": [[25, 10]]}
24-
data = napari_read_coor(coor, 'xy')
24+
data = napari_read_coor(coor, dataformat='xy')
2525

2626
assert data['germinated'][0] == (10, 25)
27+
28+
29+
def test_napari_read_coor_sam(test_data):
30+
"""Test for PlantCV.Annotate"""
31+
# Read in test data
32+
coor = {'pos': [(284, 451)], 'neg': [(206, 160)]}
33+
data = napari_read_coor(coor, dataformat='sam')
34+
35+
assert data['points'][0][0][0] == 451

0 commit comments

Comments
 (0)