CenteringCamのPythonスクリプトです。
自作ブログ時代に、WordPressのエディタを使って貼り付けたらしたら字下げが消えてしまいました。
BloggerのエディタにTab機能がないので、今回もそのままです。リストが見にくくてすみません。
では使いかたです。
1.下の #!/usr/bin/env pythonから#--END-- までを
Windowsのメモ帳またはKnoppixの
Liafpadを使って
txt形式でコピーペーストして、
CenteringCam_VerB.py というファイル名をつけて保存してください。
#!/usr/bin/env python
## -*- coding: utf-8 -*-
#========================================================
# This is the CenteringCam program that runs on Linux.
# This program runs on Python2.7 with pygame,wxPython,numpy and Opencv.
# Please install these on your Linux system in advance.
# This program requires the files "resolution.txt" , "coordinate.txt" and "manual.jpg".
# You have to write the resolution of WebCamera (you want to use)
# in to the resolution.txt.
#========================================================
#import codecs
import wx
import sys
import math
import cv
import pygame
import threading
import time
from pygame.locals import *
from pgu import text, gui as pgui
#sys.stdout = codecs.getwriter('utf_8')(sys.stdout)
#----- Global variables & defort values --------
global res1x #for resolution file reading
global res1y
global res2x
global res2y
global res3x
global res3y
global res_w
global res_h
global flag
global old_btnl #for manual btn
global btn21_data #for manual btn
global counter
global btn3text
global text1
global length_x1
global length_x2
global btn32text
global txt21
global btn12
global btn13
global btn14
global img_rgb
global Frm
global manual_set
global loaded_x1
global loaded_y1
global loaded_x2
global loaded_y2
global fix_center
fix_center="off"
f=open('resolution.txt','r')
res1x=f.readline()
res1y=f.readline()
flag=0
old_btnl="off"
btn21_data="off"
counter=1
manual_set ="off"
#res_w = int(res2x)#for VerA
#res_h = int(res2y)#for VerA
res_w = int(res1x)#for VerB
res_h = int(res1y)#for VerB
r=1
btnl="" #for all bottons
cbtn1="cb1_on" #for Fix the ratio mm/dot
cbtn2="" #for Diagonal line
cbtn3="" #for Center line
cbtn4="" #for Circle
cbtn5="" #for Fix the center
distance=0 #0mm
dot_distance=200.00 #distance of Vertical Lines
dot_data=0.01
dd=1.0
dx12=1.0
dh=25 #slider hensuu
yy=0 #for chenge y1 & y2
xx=0 #for chenge x1 & x2
lines = []
lineLimit = 5
text1=5.5
btn3text="mode2" #strt from "mode2"
btn32text="off"
window=""
#------------------------------
pygame.init()
#------------------------------
#----- definitions --------------------------------------------------------
class MyApp(wx.PySimpleApp,threading.Thread):
def OnInit(self):
Frm = wx.Frame(None, 1, "manual", size=(930,800))
ScWin = wx.ScrolledWindow(Frm, -1)
JpgImage = wx.Image("manual.jpg")
ItemSize = wx.StaticBitmap(ScWin, -1, JpgImage.ConvertToBitmap(), (0,0))
ScWin.SetScrollbars(10, 10, 60, 800)
Frm.Show()
return 1
class Manual(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.setDaemon(True)
def run(self):
global counter
global btnl
if counter==1 and btnl=="manual":
counter=2
app=MyApp()
app.MainLoop()
else:
pass
counter=1
btnl=""
#-------------------------------------------------------------------------
def logInputAction(txt):
""" add the input status to the 'edit' window (callback function)"""
global distance
text1 = txt.value
distance = text1
lines.append(distance)
while len(lines) > lineLimit: lines[0:1] = []
def logCheckAction(arg):
""" add the button status to the 'edit' window (callback function)"""
#global cbtnl
global cbtn1
global cbtn2
global cbtn3
global cbtn4
global cbtn5
btn, text = arg
if btn.value:
text += '_on';
else:
text += '_off';
cbtnl=text
if cbtnl=="cb1_on":
cbtn1="cb1_on"
else:
if cbtnl=="cb1_off":
cbtn1="cb1_off"
if cbtnl=="cb2_on":
cbtn2="cb2_on"
else:
if cbtnl=="cb2_off":
cbtn2="cb2_off"
if cbtnl=="cb3_on":
cbtn3="cb3_on"
else:
if cbtnl=="cb3_off":
cbtn3="cb3_off"
if cbtnl=="cb4_on":
cbtn4="cb4_on"
else:
if cbtnl=="cb4_off":
cbtn4="cb4_off"
if cbtnl=="cb5_on":
cbtn5="cb5_on"
else:
if cbtnl=="cb5_off":
cbtn5="cb5_off"
def logButtonAction(text):
""" add the button status to the 'edit' window (callback function)"""
global btnl
btnl=text
def logSliderAction(a):
global dh
dh=a.value
#=========== main ===========================================================
def main():
btn3text="mode2"
dx12=1.0
while 1:
global distance
global dot_distance
global btnl
global cbtn1
global cbtn2
global cbtn3
global cbtn4
global cbtn5
global img_rgb
cbtn1=""
cbtn2=""
cbtn3=""
cbtn4=""
cbtn5=""
if btn3text=="mode1" or btn3text=="mode2" or btn3text=="mode3":
distance=0 #0mm
btnl="variable on"
pygame.init()
#----- capture & screen --------------------------------------------------------------------
global res_w
global res_h
global res1x
global res1y
global res2x
global res2y
global res3x
global res3y
global btn12
global btn13
global btn14
capture = cv.CreateCameraCapture(int(0))
cv.SetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_WIDTH, res_w)
cv.SetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_HEIGHT, res_h)
pygame.init()
if res_w < 800:
screen = pygame.display.set_mode((res_w+350,res_h+160))
else:
screen = pygame.display.set_mode((res_w+350,res_h+20))
wm_info = pygame.display.get_wm_info()
handle = wm_info['window']
window = pygame.display.get_surface()
img_rgb=cv.CreateMat(res_h,res_w,cv.CV_8UC3)
#=============== coordinate ====================================
#diameter_backup=0
x1 = res_w / 2 - 150 #primary value of X1,Y1
y1 = res_h / 2 + 150
x2 = res_w / 2 + 150 #primary value of X2,Y2
y2 = res_h / 2 - 150
#============ Button & check button & slider ===================
pygame.init()
pygame.display.set_caption('Centering Cam')
#create GUI object
gui=pgui.App() #The top-level widget for an application.
lo=pgui.Container(align=0,valign=-1)#container of widgets.
txt2 = pgui.Input("?", size=4)
txt2.connect(pgui.BLUR, logInputAction, txt2)
lo.add(txt2,10+res_w+117,150)
# create checkbuttons and add to gui
cb1 = pgui.Switch()
cb1.connect(pgui.CHANGE, logCheckAction, (cb1, "cb1"))
cb1l = pgui.Label("Fix the ratio mm/dot")
cb4 = pgui.Switch()
cb4.connect(pgui.CHANGE, logCheckAction, (cb4, "cb4"))
cb4l = pgui.Label("Circle")
cb3 = pgui.Switch()
cb3.connect(pgui.CHANGE, logCheckAction, (cb3, "cb3"))
cb3l = pgui.Label("Center line")
cb2 = pgui.Switch()
cb2.connect(pgui.CHANGE, logCheckAction, (cb2, "cb2"))
cb2l = pgui.Label("Diagonal line")
cb5 = pgui.Switch()
cb5.connect(pgui.CHANGE, logCheckAction, (cb5, "cb5"))
cb5l = pgui.Label("Fix the center")
lo.add(cb1,res_w+32,210)
lo.add(cb1l,res_w+52,210)
lo.add(cb2,res_w+32,440)
lo.add(cb2l,res_w+52,440)
lo.add(cb3,res_w+32,470)
lo.add(cb3l,res_w+52,470)
lo.add(cb4,res_w+32,500)
lo.add(cb4l,res_w+52,500)
lo.add(cb5,res_w+32,240)
lo.add(cb5l,res_w+52,240)
btn21 = pgui.Button("manual")
btn21.connect(pgui.CLICK, logButtonAction, ("manual"))
btn31 = pgui.Button("save")
btn31.connect(pgui.CLICK, logButtonAction, ("save"))
btn32 = pgui.Button("load")
btn32.connect(pgui.CLICK, logButtonAction, ("load"))
if int(res1x)>999:
length_x1=50
#lo.add(btn21,10+res_w+200,75) #manual button
lo.add(btn31,10+res_w+90,75)
lo.add(btn32,10+res_w+20,75)
# create slider label
sll=pgui.Label("Radius adjustment" )
lo.add(sll,10+res_w+30,535)
# create slider
sl=pgui.HSlider(value=5,min=1,max=res_h/2,size=30,width=180,height=16)
sl.connect(pgui.CHANGE,logSliderAction,sl)
lo.add(sl,10+res_w+20,560)
gui.init(lo)
uper_screen=pygame.Surface((res_w,res_h)) #for drawing
uper_screen.set_colorkey((0,0,0)) #for transparent (0,0,0)
uper_screen.fill((0,0,0))
pygame.display.update()
#############################################################################
while 1:
global text1
if btnl=="load":# and btn3text=="mode1":
#f=open('coordinate_mode1.txt','r')#for VerA
f=open('coordinate.txt','r')#for VerB
x1=int(f.readline())
y1=int(f.readline())
x2=int(f.readline())
y2=int(f.readline())
distance=(f.readline())
txt2=f.readline()
loaded_x1=x1
loaded_y1=y1
loaded_x2=x2
loaded_y2=y2
cbtn3="cb3_on"
cbtn4="cb4_on"
global btn32text
btn32text="on"
btnl=""
#elif btnl=="load" and btn3text=="mode2":
#f=open('coordinate_mode2.txt','r')
#x1=int(f.readline())
#y1=int(f.readline())
#x2=int(f.readline())
#y2=int(f.readline())
#distance=(f.readline())
#txt2=f.readline()
#loaded_x1=x1
#loaded_y1=y1
#loaded_x2=x2
#loaded_y2=y2
#cbtn3="cb3_on"
#cbtn4="cb4_on"
#btn32text="on"
#btnl=""
#elif btnl=="load" and btn3text=="mode3":
#f=open('coordinate_mode3.txt','r')
#x1=int(f.readline())
#y1=int(f.readline())
#x2=int(f.readline())
#y2=int(f.readline())
#distance=(f.readline())
#txt2=f.readline()
#loaded_x1=x1
#loaded_y1=y1
#loaded_x2=x2
#loaded_y2=y2
#cbtn3="cb3_on"
#cbtn4="cb4_on"
#btn32text="on"
#btnl=""
#===================================================================
if btnl=="save":# and btn3text=="mode1":
#f=open('coordinate_mode1.txt','w')#for VerA
f=open('coordinate.txt','w')#for VerB
f.write(str(x1))
f.write('\n')
f.write(str(y1))
f.write('\n')
f.write(str(x2))
f.write('\n')
f.write(str(y2))
f.write('\n')
f.write(str(distance))
f.write('\n')
btnl=""
#elif btnl=="save" and btn3text=="mode2":
#f=open('coordinate_mode2.txt','w')
#f.write(str(x1))
#f.write('\n')
#f.write(str(y1))
#f.write('\n')
#f.write(str(x2))
#f.write('\n')
#f.write(str(y2))
#f.write('\n')
#f.write(str(distance))
#f.write('\n')
#btnl=""
#elif btnl=="save" and btn3text=="mode3":
#f=open('coordinate_mode3.txt','w')
#f.write(str(x1))
#f.write('\n')
#f.write(str(y1))
#f.write('\n')
#f.write(str(x2))
#f.write('\n')
#f.write(str(y2))
#f.write('\n')
#f.write(str(distance))
#f.write('\n')
#btnl=""
#====================================================================
global dd
global r
# clear setup noise, and put initial content in
lines = []
lines.append('top line of input')
lines.append('second line of input')
# Handle Input Events
for event in pygame.event.get():
if event.type == QUIT:
return
gui.event(event)
mouse_pressed = pygame.mouse.get_pressed()
if ((mouse_pressed[0]) and(btnl<>" fix on ")):
mouse_x, mouse_y = pygame.mouse.get_pos()
if mouse_x < res_w and mouse_y < res_h and mouse_y > 0:
if abs(mouse_x-x1)<abs(mouse_x-x2):
x1=mouse_x-10
else:
x2=mouse_x-10
if abs(mouse_y-y1)<abs(mouse_y-y2):
y1=mouse_y-10
else:
y2=mouse_y-10
screen.fill((188,200,219)) #(0,118,135) is light green
#======= Draw the GUI on the screen & blit the image on the window ===========================
#-----------capture ---------------------
#global img_rgb
pygame.time.delay(int(1000 * 1.0/30))
frame = cv.QueryFrame(capture)
cv.CvtColor(frame, img_rgb, cv.CV_BGR2RGB)
pg_img = pygame.image.frombuffer(img_rgb.tostring(), cv.GetSize(img_rgb), "RGB")
#---------- after fill the screen with lighe green blit image to screen ----------
screen.fill((188,200,219)) #(0,118,135) is light green
screen.blit(pg_img, (10,10))
#---------- rewrite(=clear) uper_screen ------------------------------------------
uper_screen.fill((0,0,0))
#---------- drawing the lines & circle & others ----------------------------------
if y1<y2:
yy=y2
y2=y1
y1=yy
if x1>x2:
xx=x2
x2=x1
x1=xx
#pygame.event.pump() #
pressed = pygame.key.get_pressed()
if pressed[K_r]:
y1=y1-1
if pressed[K_c]:
y1=y1+1
if pressed[K_i]:
y2=y2-1
if pressed[K_m]:
y2=y2+1
if pressed[K_d]:
x1=x1-1
if pressed[K_f]:
x1=x1+1
if pressed[K_j]:
x2=x2-1
if pressed[K_k]:
x2=x2+1
pygame.draw.line(uper_screen, (255,0,0), (x1,0), (x1,res_h+10)) #(255,0,0)=red
pygame.draw.line(uper_screen, (255,0,0), (0,y1), (res_w+10,y1))
pygame.draw.line(uper_screen, (255,0,0), (x2,0), (x2,res_h+10))
pygame.draw.line(uper_screen, (255,0,0), (0,y2), (res_w+10,y2))
global fix_center
if (cbtn5=="cb5_on") and (fix_center=="off"):
loaded_x1=x1
loaded_y1=y1
loaded_x2=x2
loaded_y2=y2
fix_center="on"
if (cbtn5<>"cb5_on"):
fix_center="off"
###----------------circle---------------------###
if cbtn4=="cb4_on" and fix_center=="on":
r=dh
pygame.draw.circle(uper_screen,(241,139,140),((loaded_x1+loaded_x2+2)/2,(loaded_y1+loaded_y2+2)/2), int(r),1)
elif cbtn4=="cb4_on":
r=dh
pygame.draw.circle(uper_screen,(255,0,0),((x1+x2+2)/2,(y1+y2+2)/2), int(r),1)
###-----------------center line----------------------###
if cbtn3=="cb3_on" and fix_center=="on":
pygame.draw.line(uper_screen,(241,139,140),((loaded_x1+loaded_x2)/2,0),((loaded_x1+loaded_x2)/2,res_h))
pygame.draw.line(uper_screen,(241,139,140),(0,(loaded_y1+loaded_y2)/2),(res_w,(loaded_y1+loaded_y2)/2))
elif cbtn3=="cb3_on":
pygame.draw.line(uper_screen,(200,255,0),((x1+x2)/2,0),((x1+x2)/2,res_h)) #(200,255,0)=yellow
pygame.draw.line(uper_screen,(200,255,0),(0,(y1+y2)/2),(res_w,(y1+y2)/2))
###------------------Diagonal line----------------------------###
if cbtn2=="cb2_on" and fix_center=="on":
pygame.draw.line(uper_screen,(241,139,140), (loaded_x1,loaded_y1), (loaded_x2,loaded_y2))
pygame.draw.line(uper_screen, (241,139,140), (loaded_x1,loaded_y2), (loaded_x2,loaded_y1))
elif cbtn2=="cb2_on":
pygame.draw.line(uper_screen, (255,0,0), (x1,y1), (x2,y2))
pygame.draw.line(uper_screen, (255,0,0), (x1,y2), (x2,y1))
#""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
if cbtn1<>"cb1_on" :
dx12=abs(x2-x1)
try:
dot_distance=round((float(distance))/dx12,6)
gui.paint(screen)
except:
font=pygame.font.Font(None,20)
text3=font.render("Only the figure",True,(150,150,40))
if btn32text<>"on":
window.blit(text3,(10+res_w+135,177))
gui.paint(screen)
btn32text=""
if btn3text<>"mode1" or btn3text<>"mode2" or btn3text<>"mode3":
if btnl=="mode1":
btn3text=btnl
res_w=int(res1x)
res_h=int(res1y)
btnl=""
break
if btnl=="mode2":
btn3text=btnl
res_w=int(res2x)
res_h=int(res2y)
btnl=""
break
if btnl=="mode3":
btn3text=btnl
res_w=int(res3x)
res_h=int(res3y)
btnl=""
break
else:
res_w=int(res2x)
res_h=int(res2y)
dot_data=round(dot_distance,8)
dd=str(dot_data)
diameter=round(2.0*float(r)*float(dd),2)
font=pygame.font.Font(None,24)
text11=font.render("Scale",True,(0,0,0))
window.blit(text11,(10+res_w+150,120))
text1=font.render("L <--> R = ",True,(0,0,0))
window.blit(text1,(10+res_w+67,155))
text2=font.render("mm",True,(0,0,0))
window.blit(text2,(10+res_w+207,155))
text3=font.render((dd),True,(255,0,0))
window.blit(text3,(10+res_w+167,190))
text4=font.render("--> mm/dot",True,(0,0,0))
window.blit(text4,(10+res_w+137,189))
font=pygame.font.Font(None,30)
if cbtn1<>"cb1_on" and cbtn5<>"cb5_on":
text7=font.render(str(round((abs(x1-x2)*dot_data),3)),True,(0,0,0))
if (y1>=y2):
window.blit(text7,((x1+x2)/2+20,y1+15))
else:
window.blit(text7,((x1+x2)/2+20,y2+15))
#--- if check_box1 (Fix the ratio mm/dot) on ---
if cbtn1=="cb1_on":
text7=font.render(str(round((abs(x1-x2)*dot_data),3)),True,(255,0,0))
if (y1>=y2):
window.blit(text7,((x1+x2)/2+20,y1+15))
else:
window.blit(text7,((x1+x2)/2+20,y2+15))
text8=font.render(str(round((abs(y1-y2)*dot_data),3)),True,(255,0,0))
if x1<=x2 and x1>60:
window.blit(text8,(x1-55,(y1+y2)/2+20))
elif x1<=x2 and x1<=60:
window.blit(text8,(x1+15,(y1+y2)/2+20))
elif x2<=x1 and x2>60:
window.blit(text8,(x1-55,(y1+y2)/2+20))
elif x2<=x1 and x1<=60:
window.blit(text8,(x1+15,(y1+y2)/2+20))
#--- if check_box4 (Circle) ---
if (cbtn4=="cb4_on"):
text5=font.render("Diameter = mm",True,(0,0,0))
window.blit(text5,(10+res_w+60,585))
font=pygame.font.Font(None,30)
text6=font.render(str(diameter),True,(255,0,0))
window.blit(text6,(10+res_w+180,585)) #data of diameter
font=pygame.font.Font(None,55)
text_title=font.render("Centering Cam",True,(150,150,40))
window.blit(text_title,(10+res_w+30,20))
#---------------- line of menu ------------------------------------------
font=pygame.font.Font(None,24)
text11=font.render("Scale",True,(0,0,0))
window.blit(text11,(10+res_w+150,120))
pygame.draw.line(window, (0,50,135), (res_w+50,126), (res_w+130,126)) #0,118,135
pygame.draw.line(window, (0,50,135), (res_w+230,126), (res_w+310,126))
text11=font.render("Centering",True,(0,0,0))
window.blit(text11,(10+res_w+128,283))
pygame.draw.line(window, (0,50,135), (res_w+50,290), (res_w+130,290)) #0,118,135
pygame.draw.line(window, (0,50,135), (res_w+230,290), (res_w+310,290))
#----------------account of key -------------------------------
font=pygame.font.Font(None,16)
#text11=font.render("A+UP arrow",True,(0,0,0))
text11=font.render("R (UP)",True,(0,0,0))
window.blit(text11,(10+res_w+75,370))
#text11=font.render("A+DOWN arrow",True,(0,0,0))
text11=font.render("C (DOWN)",True,(0,0,0))
window.blit(text11,(10+res_w+75,380))
#text11=font.render("A+LEFT arrow",True,(0,0,0))
text11=font.render("D (LEFT)",True,(0,0,0))
window.blit(text11,(10+res_w+75,400))
#text11=font.render("A+RIGHT arrow",True,(0,0,0))
text11=font.render("F (RIGHT)",True,(0,0,0))
window.blit(text11,(10+res_w+75,410))
pygame.draw.line(window, (0,50,135), (res_w+65,395), (res_w+170,395)) #0,118,135
pygame.draw.line(window, (0,50,135), (res_w+150,370), (res_w+150,420))
text11=font.render("I (UP)",True,(0,0,0))
window.blit(text11,(10+res_w+218,310))
text11=font.render("M (DOWN)",True,(0,0,0))
window.blit(text11,(10+res_w+215,320))
text11=font.render("J (LEFT)",True,(0,0,0))
window.blit(text11,(10+res_w+215,340))
text11=font.render("K (RIGHT)",True,(0,0,0))
window.blit(text11,(10+res_w+215,350))
pygame.draw.line(window, (0,50,135), (res_w+195,335), (res_w+300,335)) #0,118,135
pygame.draw.line(window, (0,50,135), (res_w+215,310), (res_w+215,360))
text11=font.render("or",True,(0,0,0))
window.blit(text11,(10+res_w+170,405))
text11=font.render("MOUSE(LEFT BUTTON)",True,(0,0,0))
window.blit(text11,(10+res_w+175,415))
window.blit(uper_screen,(10,10))
pygame.display.flip()
global counter
if counter==2:
pass
elif btnl=="manual" and counter==1:
M=Manual()
M.start()
#================================================================================
main()
#--END--
2.resolution.txtファイルです。下の640からThe 2nd line is the direction of y. までを
Windowsのメモ帳またはKnoppixの
Liafpadを使って
txt形式でコピーペーストして、
resolution.txt というファイル名をつけて保存してください。
640
480
The 1st line is the direction of x.
The 2nd line is the direction of y.
3.coordinate.txtファイルです。下の138 から 6 までを
Windowsのメモ帳またはKnoppixの
Liafpadを使って
txt形式でコピーペーストして、
coordinate.txt というファイル名をつけて保存してください。
138
375
489
78
6
4.manual.jpgです。下の画像をクリックしてコピーし
manual.jpgという名前で画像を保存してください。resolution.txt 、 coordinate.txt と一緒(
同じホルダー)に置いてください。
manualの画像はWindowsバージョンでのみ機能します。windowsバージョンには解像度切り替えボタンがついています。Linuxでは切り替えがうまく機能しなかったのでこの機能は削除しました。
以上です。