Skip to content
This repository has been archived by the owner on Jun 19, 2020. It is now read-only.

Commit

Permalink
enable color_profile save
Browse files Browse the repository at this point in the history
  • Loading branch information
Weijian Zeng committed Jan 24, 2020
1 parent df632e7 commit 5b07870
Show file tree
Hide file tree
Showing 6 changed files with 301 additions and 174 deletions.
4 changes: 2 additions & 2 deletions profiles/color_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ def to_encodable(self):
max=self.hsv_hue.max),
s=dict(min=self.hsv_sat.min,
max=self.hsv_sat.max),
v=dict(min=self.hsv_hue.min,
max=self.hsv_hue.max)
v=dict(min=self.hsv_val.min,
max=self.hsv_val.max)
)

return dict(camera_mode=self.camera_mode,
Expand Down
20 changes: 11 additions & 9 deletions start_web.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
#!/usr/bin/env python3

import network as networktables
from web import tornado_server

import logging
# initiate the top level logger
# logging.basicConfig(
# level=logging.INFO,
# format="%(asctime)s [%(name)s] [%(levelname)-5.5s] %(message)s",
# handlers=[
# logging.StreamHandler()
# ]
# )
#initiate the top level logger

logger = logging.getLogger('app')


def main():

tornado_server.start()

if __name__ == '__main__':

logging.basicConfig(
level=logging.INFO,
format="%(asctime)s [%(name)s] [%(levelname)-5.5s] %(message)s",
handlers=[
logging.StreamHandler()
]
)
main()
45 changes: 39 additions & 6 deletions web/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
from profiles.color_profile import ColorProfileEncoder
from .nt_serial import NTSerial

root_path = abspath(join(dirname(__file__),"../"))

logger = logging.getLogger("handlers")

USE_NT_TABLES = False
Expand Down Expand Up @@ -75,9 +77,12 @@ def open(self):
if USE_NT_TABLES:
self.ntserial = NTSerial(self.send_msg_threadsafe)

camera = dict(width=self.application.settings['camera'].WIDTH, height=self.application.settings['camera'].HEIGHT)
logger.info(camera)
self.write_message(json_encode.dumps(dict(socket=self.uid,
camera=camera,
color_profiles=self.application.settings['color_profiles']),
cls=ColorProfileEncoder))
cls=ColorProfileEncoder))

def check_origin(self, origin):
"""
Expand All @@ -99,14 +104,25 @@ def on_message(self, message):
dashboard.putValue(networktables.keys.vision_camera_mode, controls['camera_mode'])

elif 'color_profile' in inputs:

profile = inputs['color_profile']

if 'reset' in inputs:
file_name = 'color_profile_%s.json' % (profile['camera_mode'])
filepath = join(root_path, 'profiles', file_name)
logger.info('loading profile from %s' % filepath)
try:
with open(filepath, mode='rb') as f:
profile = json.loads(f.read())
except:
logger.error('missing file %s' % filepath )

if USE_NT_TABLES:
dashboard.putValue(networktables.keys.vision_color_profile, json.dumps(profile))

color_profile = self.application.settings['color_profiles'].get(profile['camera_mode'])
logger.info('updating color profile for %s' % color_profile.camera_mode)


color_profile.red.min = int(profile['rgb']['r']['min'])
color_profile.red.max = int(profile['rgb']['r']['max'])

Expand All @@ -125,10 +141,27 @@ def on_message(self, message):
color_profile.hsv_val.min = int(profile['hsv']['v']['min'])
color_profile.hsv_val.max = int(profile['hsv']['v']['max'])

#
# color_profile.update(rgb=color_profile['rgb'],
# hsl=color_profile['hsl'],
# hsv=color_profile['hsv'])
color_profile.hsl_hue.min = int(profile['hsl']['h']['min'])
color_profile.hsl_hue.max = int(profile['hsl']['h']['max'])

color_profile.hsl_sat.min = int(profile['hsl']['s']['min'])
color_profile.hsl_sat.max = int(profile['hsl']['s']['max'])

color_profile.hsl_lum.min = int(profile['hsl']['l']['min'])
color_profile.hsl_lum.max = int(profile['hsl']['l']['max'])

if 'reset' in inputs:
self.write_message(json_encode.dumps(dict(socket=self.uid,
color_profiles=self.application.settings['color_profiles']),
cls=ColorProfileEncoder))

if 'save' in inputs:
file_name = 'color_profile_%s.json' % (profile['camera_mode'])
filepath = join(root_path, 'profiles', file_name)
logger.info('writing profile to %s' % filepath)
with open(filepath, mode='w') as f:
json.dump(profile, f, indent=4)


logger.info('broadcasting to %s' % len(DashboardWebSocket.watchers))
for watcher in DashboardWebSocket.watchers:
Expand Down
96 changes: 49 additions & 47 deletions web/image_stream_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,16 +122,57 @@ def read_image_loop(application):
if interval > 0:
if len(application.settings['sockets']):
_, image = cam.read()
image = cv2.resize(image, ((int)(640), (int)(480)), 0, 0, cv2.INTER_CUBIC)

image = cv2.resize(image, ((int)(640), (int)(400)), 0, 0, cv2.INTER_CUBIC)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)



if ImagePushStreamHandler.camera_mode != 'RAW':



color_profile = application.settings['color_profiles'].get(ImagePushStreamHandler.camera_mode)

mask = None


if ImagePushStreamHandler.color_mode == 'rgb':
mask = cv2.inRange(image,
(color_profile.red.min, color_profile.green.min, color_profile.blue.min),
(color_profile.red.max, color_profile.green.max, color_profile.blue.max))

elif ImagePushStreamHandler.color_mode == 'hsv':
hue = color_profile.hsv_hue
sat = color_profile.hsv_sat
val = color_profile.hsv_val

hsv = cv2.cvtColor(image, cv2.COLOR_RGB2HSV)
mask = cv2.inRange(hsv, (hue.min, sat.min, val.min), (hue.max, sat.max, val.max))
#
# elif ImagePushStreamHandler.color_mode == 'hsl':
# hue = color_profile.hsl_lum
# sat = color_profile.hsl_lum
# lum = color_profile.hsl_lum
# hsl = cv2.cvtColor(image, cv2.COLOR_BGR2HSL)
# hsl = cv2.inRange(image, (hue.min, sat.min, lum.min), (hue.max, sat.max, lum.max))

if mask is not None:
if ImagePushStreamHandler.apply_mask:
image = cvfilters.apply_mask(image, mask)
image = cv2.erode(image, None, iterations=2)
image = cv2.dilate(image, None, iterations=2)
else:
image = mask

cv2.putText(image,
'Mode %s' % ImagePushStreamHandler.camera_mode,
(20,20),
cv2.FONT_HERSHEY_DUPLEX,
.4,
colors.BLUE,
1,
cv2.LINE_AA)
'Mode %s' % ImagePushStreamHandler.camera_mode,
(20,20),
cv2.FONT_HERSHEY_DUPLEX,
.4,
colors.BLUE,
1,
cv2.LINE_AA)

if ImagePushStreamHandler.color_mode is not None:
cv2.putText(image,
Expand All @@ -142,45 +183,6 @@ def read_image_loop(application):
colors.BLUE,
1,
cv2.LINE_AA)

color_profile = application.settings['color_profiles'].get(ImagePushStreamHandler.camera_mode)

mask = None

image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

if ImagePushStreamHandler.color_mode == 'rgb':
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
mask = cv2.inRange(image,
(color_profile.red.min, color_profile.green.min, color_profile.blue.min),
(color_profile.red.max, color_profile.green.max, color_profile.blue.max))

elif ImagePushStreamHandler.color_mode == 'hsv':
hue = color_profile.hsv_hue
sat = color_profile.hsv_sat
val = color_profile.hsv_val

logger.info((hue.min, sat.min, val.min))
logger.info((hue.max, sat.max, val.max))

hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv, (hue.min, sat.min, val.min), (hue.max, sat.max, val.max))
#
# elif ImagePushStreamHandler.color_mode == 'hsl':
# hue = color_profile.hsl_lum
# sat = color_profile.hsl_lum
# lum = color_profile.hsl_lum
# hsl = cv2.cvtColor(image, cv2.COLOR_BGR2HSL)
# hsl = cv2.inRange(image, (hue.min, sat.min, lum.min), (hue.max, sat.max, lum.max))

if mask is not None:
if ImagePushStreamHandler.apply_mask:
image = cvfilters.apply_mask(image, mask)
image = cv2.erode(image, None, iterations=2)
image = cv2.dilate(image, None, iterations=2)
else:
image = mask

jpg = convert_to_jpg(image)
for ws in application.settings['sockets']:
ws.images.append(jpg)
Expand Down
Loading

0 comments on commit 5b07870

Please sign in to comment.