Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions SerialController/Camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,22 @@ def readFrame(self):
_, self.image_bgr = self.camera.read()
return self.image_bgr

def saveCapture(self):
# box(for trim) format: (left, right, up, bottom)
def saveCapture(self, box=None):
dt_now = datetime.datetime.now()
fileName = dt_now.strftime('%Y-%m-%d_%H-%M-%S')+".png"

if not os.path.exists(self.capture_dir):
os.makedirs(self.capture_dir)

save_path = os.path.join(self.capture_dir, fileName)
cv2.imwrite(save_path, self.image_bgr)
print('capture succeeded: ' + save_path)

try:
image = self.image_bgr if box is None else self.image_bgr[box[2]:box[3], box[0]:box[1]]
cv2.imwrite(save_path, image)
print('capture succeeded: ' + save_path)
except:
print('capture failed')

def destroy(self):
if self.camera is not None and self.camera.isOpened():
Expand Down
20 changes: 20 additions & 0 deletions SerialController/GuiAssets.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ def __init__(self, camera, fps, is_show, master=None):
self.camera = camera
self.show_size = (640, 360)
self.is_show_var = is_show
self.is_trim_mode = False

self.setFps(fps)
self.bind("<ButtonPress-1>", self.mouseLeftDown)
self.bind("<ButtonRelease-1>", self.mouseLeftUp)

# Set disabled image first
disabled_img = cv2.imread("../Images/disabled.png", cv2.IMREAD_GRAYSCALE)
Expand All @@ -33,6 +35,24 @@ def mouseLeftDown(self, event):
ratio_x = float(self.camera.capture_size[0] / self.show_size[0])
ratio_y = float(self.camera.capture_size[1] / self.show_size[1])
print('mouse down: show ({}, {}) / capture ({}, {})'.format(x, y, int(x * ratio_x), int(y * ratio_y)))

self.is_trim_mode = True
self.down_trim_x = int(x * ratio_x)
self.down_trim_y = int(y * ratio_y)

def mouseLeftUp(self, event):
x, y = event.x, event.y
ratio_x = float(self.camera.capture_size[0] / self.show_size[0])
ratio_y = float(self.camera.capture_size[1] / self.show_size[1])
up_trim_x = int(x * ratio_x)
up_trim_y = int(y * ratio_y)
print('mouse up: show ({}, {}) / capture ({}, {})'.format(x, y, up_trim_x, up_trim_y))

if self.is_trim_mode:
self.camera.saveCapture(
(min(self.down_trim_x, up_trim_x), max(self.down_trim_x, up_trim_x),
min(self.down_trim_y, up_trim_y), max(self.down_trim_y, up_trim_y)))
self.is_trim_mode = False

def startCapture(self):
self.capture()
Expand Down