import wx
from wx.lib import statbmp
import cv, cv2
import numpy as np
import os
import traceback
class ChooseSaveDialog(wx.Dialog):
def __init__(self, parent):
wx.Dialog.__init__(self, parent)
panel = wx.Panel(self, -1)
sizer = wx.BoxSizer(wx.VERTICAL)
btn1 = wx.Button(panel, -1, 'Save Original Image')
btn2 = wx.Button(panel, -1, 'Save ROI Image')
btn3 = wx.Button(panel, -1, 'Save Both')
sizer.Add(btn1, 0)
sizer.Add(btn2, 0)
sizer.Add(btn3, 0)
panel.SetSizer(sizer)
btn1.Bind(wx.EVT_BUTTON, self.save_orig)
btn2.Bind(wx.EVT_BUTTON, self.save_roi)
btn3.Bind(wx.EVT_BUTTON, self.save_both)
def save_orig(self, evt):
save_dialog = wx.FileDialog(self, "Save Original to JPEG", "", "", "JPG files(*.jpg)", wx.FD_SAVE|wx.FD_OVERWRITE_PROMPT)#|wx.FD_CHANGE_DIR)
if save_dialog.ShowModal() == wx.ID_OK:
save_path = save_dialog.GetPath()
if save_path[:-4].lower() != '.jpg':
save_path += '.jpg'
print save_path
cv2.imwrite(save_path, self.GetParent().orig_frame)
self.EndModal(wx.OK)
def save_roi(self, evt):
save_dialog = wx.FileDialog(self, "Save ROI to JPEG", "", "", "JPG files(*.jpg)", wx.FD_SAVE|wx.FD_OVERWRITE_PROMPT)#|wx.FD_CHANGE_DIR)
if save_dialog.ShowModal() == wx.ID_OK:
save_path = save_dialog.GetPath()
if save_path[:-4].lower() != '.jpg':
save_path += '.jpg'
print save_path
cv2.imwrite(save_path, self.GetParent().frameRoi)
try:
self.EndModal(wx.OK)
except wx.PyAssertionError:
pass
def save_both(self, evt):
self.save_orig(evt)
self.save_roi(evt)
import wx.lib.scrolledpanel as scrolled
class ShowCapture(wx.Frame):
def __init__(self, capture, fps=15):
wx.Frame.__init__(self, None)
panel = scrolled.ScrolledPanel(self, -1) #wx.Panel(self, -1)
panel.SetupScrolling()
framesizer = wx.BoxSizer(wx.VERTICAL)
#create a grid sizer with 5 pix between each cell
sizer = wx.GridBagSizer(5, 5)
self.capture = capture
ret, frame = self.capture.read()
height, width = frame.shape[:2]
print 'height %s, width %s' % (height, width)
self.orig_height = height
self.orig_width = width
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
self.bmp = wx.BitmapFromBuffer(width, height, frame)
self.dummy_element = wx.TextCtrl(panel, -1,'')
self.dummy_element.Hide()
#create SpinCtrl widgets, these have vertical up/down buttons and a TextCtrl that increments with up/down press
self.roi_x = wx.SpinCtrl(panel, -1, "ROI X", style=wx.TE_PROCESS_ENTER|wx.SP_ARROW_KEYS, min=0, max=width, initial=0, size=(60,-1))
self.roi_y = wx.SpinCtrl(panel, -1, "ROI Y", style=wx.TE_PROCESS_ENTER|wx.SP_ARROW_KEYS, min=0, max=height, initial=0, size=(60,-1))
self.roi_width = wx.SpinCtrl(panel, -1, "ROI W", style=wx.TE_PROCESS_ENTER|wx.SP_ARROW_KEYS, min=0, max=width, initial=width, size=(60,-1))
self.roi_height = wx.SpinCtrl(panel, -1, "ROI H", style=wx.TE_PROCESS_ENTER|wx.SP_ARROW_KEYS, min=0, max=height, initial=height, size=(60,-1))
save_bmp_path = os.path.join(os.path.dirname(__file__), 'icons', 'ic_action_save.png')
if os.path.isfile(save_bmp_path):
save_bmp = wx.Image(save_bmp_path).ConvertToBitmap()
save_button = wx.BitmapButton(panel,wx.ID_ANY, bitmap=save_bmp, style = wx.NO_BORDER, size=(32,32)) # )
else:
save_button = wx.Button(panel, -1, 'Save')
settings_bmp_path = os.path.join(os.path.dirname(__file__), 'icons', 'ic_action_settings.png')
if os.path.isfile(settings_bmp_path):
settings_bmp = wx.Image(settings_bmp_path).ConvertToBitmap()
settings_button = wx.BitmapButton(panel,wx.ID_ANY, bitmap=settings_bmp, style = wx.NO_BORDER, size=(32,32)) # )
else:
settings_button = wx.Button(panel, -1, 'Settings')
#create image display widgets
self.ImgControl = statbmp.GenStaticBitmap(panel, wx.ID_ANY, self.bmp)
self.ImgControl2 = statbmp.GenStaticBitmap(panel, wx.ID_ANY, self.bmp)
#add text to the sizer grid
sizer.Add(wx.StaticText(panel, -1, 'ROI'), (0, 0), (1,2), wx.ALL, 5)
sizer.Add(wx.StaticText(panel, -1, 'X'), (1, 0), wx.DefaultSpan, wx.ALL, 5)
sizer.Add(wx.StaticText(panel, -1, 'Y'), (2, 0), wx.DefaultSpan, wx.ALL, 5)
sizer.Add(wx.StaticText(panel, -1, 'width,'), (1, 2), wx.DefaultSpan, wx.ALL, 5)
sizer.Add(wx.StaticText(panel, -1, 'height'), (2, 2), wx.DefaultSpan, wx.ALL, 5)
sizer.Add(wx.StaticText(panel, -1, 'Right-click image to reset ROI'), (2, 4), (1,2), wx.ALL, 5)
tool_button_sizer = wx.BoxSizer(wx.HORIZONTAL)
tool_button_sizer.Add(save_button, 0)
tool_button_sizer.Add(settings_button, 0)
#sizer.Add(, (0, 6), wx.DefaultSpan, wx.ALIGN_RIGHT)#, wx.ALL, 5)
sizer.Add(tool_button_sizer, (0, 4), (1, 4), wx.ALIGN_RIGHT)#, wx.ALL, 5)
#add SpinCtrl widgets to the sizer grid
sizer.Add(self.roi_x, (1, 1), wx.DefaultSpan, wx.ALL, 5)
sizer.Add(self.roi_y, (2, 1), wx.DefaultSpan, wx.ALL, 5)
sizer.Add(self.roi_width, (1, 3), wx.DefaultSpan, wx.ALL, 5)
sizer.Add(self.roi_height, (2, 3), wx.DefaultSpan, wx.ALL, 5)
#add image widgets to the sizer grid
sizer.Add(self.ImgControl, (3, 0), (1,4), wx.EXPAND|wx.CENTER|wx.LEFT|wx.BOTTOM, 5)
sizer.Add(self.ImgControl2, (3, 4), (1,4), wx.EXPAND|wx.CENTER|wx.RIGHT|wx.BOTTOM, 5)
#set the sizer and tell the Frame about the best size
panel.SetSizer(sizer)
#sizer.SetSizeHints(self)
panel.Layout()
panel.SetFocus()
self.panel = panel
self.sizer = sizer
#framesizer.Add(panel)
#self.SetSizer(framesizer)
#start a timer that's handler grabs a new frame and updates the image widgets
self.timer = wx.Timer(self)
self.fps = fps
self.timer.Start(1000./self.fps)
#bind timer events to the handler
self.Bind(wx.EVT_TIMER, self.NextFrame)
#bind events to ROI image widget
self.ImgControl2.Bind(wx.EVT_LEFT_DOWN, self.On_ROI_Click)
self.ImgControl2.Bind(wx.EVT_LEFT_UP, self.On_ROI_ClickRelease)
self.ImgControl2.Bind(wx.EVT_RIGHT_DOWN, self.On_ROI_RightClick)
self.ImgControl2.Bind(wx.EVT_MOTION, self.On_ROI_Hover)
self.ImgControl2.Bind(wx.EVT_ENTER_WINDOW, self.On_ROI_mouse_enter)
self.ImgControl2.Bind(wx.EVT_LEAVE_WINDOW, self.On_ROI_mouse_leave)
#bind save button
save_button.Bind(wx.EVT_BUTTON, self.on_save_click)
save_button.Bind(wx.EVT_RIGHT_DOWN, self.on_quick_save)
#bind settings button
settings_button.Bind(wx.EVT_BUTTON, self.on_settings_click)
settings_button.Bind(wx.EVT_LEFT_UP, self.on_settings_click_release)
def on_settings_click(self, evt):
pass
def on_settings_click_release(self, evt):
self.dummy_element.SetFocus()
def on_save_click(self, evt):
modal = ChooseSaveDialog(self)
self.timer.Stop()
modal.ShowModal()
modal.Destroy()
self.timer.Start(1000./self.fps)
def on_quick_save(self, evt):
cv2.imwrite('orig_frame.jpg', self.orig_frame)
cv2.imwrite('frameRoi.jpg', self.frameRoi)
def On_ROI_RightClick(self, evt):
self.roi_x.SetValue(0)
self.roi_y.SetValue(0)
self.roi_width.SetValue(self.orig_width)
self.roi_height.SetValue(self.orig_height)
def On_ROI_Hover(self, evt):
self.ROI_crosshair_pos = evt.GetPosition()
def On_ROI_mouse_enter(self, evt):
self
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
py_cv.tar.gz (1个子文件)
py_cv.py 12KB
共 1 条
- 1
资源评论
钱亚锋
- 粉丝: 101
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功