Skip to content

Commit 59683b7

Browse files
committed
adding real-time contact area extraction example
1 parent 6159b8f commit 59683b7

File tree

2 files changed

+44
-10
lines changed

2 files changed

+44
-10
lines changed

examples/contact_area.py

+29-1
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,33 @@ def extract_surface_contact():
2929
ImageHandler.save("surface_contact_2.png", sample_img_2)
3030

3131

32+
def extract_surface_contact_real_time():
33+
"""
34+
This function demonstrates how to use the ContactArea task with real-time mode with DIGIT sensor.
35+
Make sure to have a DIGIT sensor connected to your computer and setup the digit-interface library.
36+
Output:
37+
Displays a real-time object contact area by fitting an ellipse.
38+
"""
39+
from digit_interface import Digit
40+
import cv2
41+
base_img_path = "/home/shuk/digits2/images/0000/0041.png"
42+
base_img = ImageHandler(base_img_path).nparray
43+
# setting up the DIGIT sensor
44+
digit = Digit("Dxxxxx") # replace xxxxx with your DIGIT serial number
45+
digit.connect()
46+
while True:
47+
frame=digit.get_frame()
48+
img=cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # target image frame for __call__ method
49+
contact_area=ContactArea(base=base_img, contour_threshold=10,real_time=True)
50+
contact_area(target=img) # __call__ method
51+
cv2.imshow('surface contact', img)
52+
k=cv2.waitKey(1)
53+
if k%256==27:
54+
#ESC hit
55+
print("Escape hit, closing...")
56+
break
57+
58+
3259
if __name__ == "__main__":
33-
extract_surface_contact()
60+
#extract_surface_contact()
61+
extract_surface_contact_real_time()

pytouch/tasks/contact_area.py

+15-9
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@
1010

1111
class ContactArea:
1212
def __init__(
13-
self, base=None, draw_poly=True, contour_threshold=100, *args, **kwargs
13+
self, base=None, draw_poly=True, contour_threshold=100,real_time=False,*args, **kwargs
1414
):
1515
self.base = base
1616
self.draw_poly = draw_poly
1717
self.contour_threshold = contour_threshold
18+
self.real_time = real_time
1819

1920
def __call__(self, target, base=None):
2021
base = self.base if base is None else base
@@ -23,13 +24,18 @@ def __call__(self, target, base=None):
2324
diff = self._diff(target, base)
2425
diff = self._smooth(diff)
2526
contours = self._contours(diff)
26-
(
27-
poly,
28-
major_axis,
29-
major_axis_end,
30-
minor_axis,
31-
minor_axis_end,
32-
) = self._compute_contact_area(contours, self.contour_threshold)
27+
if self._compute_contact_area(contours, self.contour_threshold) == None and self.real_time==False:
28+
raise Exception("No contact area detected.")
29+
if self._compute_contact_area(contours, self.contour_threshold) == None and self.real_time==True:
30+
return None
31+
else:
32+
(
33+
poly,
34+
major_axis,
35+
major_axis_end,
36+
minor_axis,
37+
minor_axis_end,
38+
) = self._compute_contact_area(contours, self.contour_threshold)
3339
if self.draw_poly:
3440
self._draw_major_minor(
3541
target, poly, major_axis, major_axis_end, minor_axis, minor_axis_end
@@ -104,4 +110,4 @@ def _compute_contact_area(self, contours, contour_threshold):
104110
)
105111
major_axis_end = 2 * center - major_axis
106112
minor_axis_end = 2 * center - minor_axis
107-
return poly, major_axis, major_axis_end, minor_axis, minor_axis_end
113+
return poly, major_axis, major_axis_end, minor_axis, minor_axis_end

0 commit comments

Comments
 (0)