GANAN714 05月27日
关于Kivy传入图片名称的问题
问题:请问kivy图片的”source“可以用变量赋值吗?如果可以的话,应该怎么做?
如图,当我直接给source赋值字符串时:source:”HDR/fusion_xxx.png“时可以正常显示,
但是当我用变量赋值时,不能正常显示。


全部代码:
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder
from kivy.uix.image import Image
from kivy.lang import Builder
from kivy.uix.label import Label
import time
import kivy
import cv2 as cv
import numpy as np
import argparse
import os
import cv2
from kivy.clock import Clock
from kivy.graphics.texture import Texture
from kivy.core.window import Window
import pygame.camera
import pygame.image

Builder.load_string("""
<CameraScreen>:
BoxLayout:
orientation: 'vertical'
Camera:
id: sjnCamera
play: True
index: 0
width:'60dp'
height:'80dp'
# Preview:
# id: qrcam

BoxLayout:
orientation:'horizontal'
size_hint:1,.1
Button:
text:'camera0'
background_color: 1,1,0,1
on_press: root.selectCamera(0)
Button:
text:'camera1'
background_color: 1,1,0,1
on_press: root.selectCamera(1)
Button:
text:'camera2'
background_color: 1,1,0,1
on_press: root.selectCamera(2)
Button:
text:'camera3'
background_color: 1,1,0,1
on_press: root.selectCamera(3)

BoxLayout:
orientation:'horizontal'
size_hint:1,.1
Button:
text:'1 stop'
background_color: 1,1,0,1
on_press: root.selectCompensation(1)
Button:
text:'2 stop'
background_color: 1,1,0,1
on_press: root.selectCompensation(2)
Button:
text:'3 stop'
background_color: 1,1,0,1
on_press: root.selectCompensation(3)

BoxLayout:
orientation:'horizontal'
size_hint_y: None
height: '48dp'
Button:
text: 'Display'
on_press: root.manager.current = 'photo_screen'
Button:
text: 'Shoot'
on_press: root.takePicture()
Button:
text: 'createCamera'
#on_press: root.createCamera()

<PhotoScreen>:
display_HDR_image:"/HDR/fusion_175225.png"
BoxLayout:
orientation: 'vertical'

# Image:
# source:root.display_HDR_image
# id:HDR_display
# size_hint:1,.9

Button:
id:btn1
size_hint:1,.9
#dhi:"/HDR/fusion_175225.png"
canvas.after:
Rectangle:
id:HDR_display
pos:self.pos
size:self.size
source:root.display_HDR_image
#source:self.dhi
#source:"HDR/fusion_175225.png"
# Image:
# width:'60dp'
# height:'80dp'
# source:root.display_HDR_image


# Image:
# id:HDR_display
# size_hint:1,.9
# allow_stretch:True
# source:root.display_HDR_image

# canvas:
# Rectangle:
# #size_hint:1,.9
# #size:self.width+20,self.height+20
# pos:self.pos
# source:root.display_HDR_image
# Preview:
# id: qrcam
# canvas:
# color:
# rgba:[1,1,1,1]
# Rectangle:
# size:self.width+20,self.height+20
# pos:self.pos
# source:root.display_HDR_image

BoxLayout:
orientation:'horizontal'
size_hint:1,.1
Button:
text: 'left'
on_press: root.left()
Button:
text: 'right'
on_press: root.right()
Button:
text: 'Back'
on_press:
root.exit_display()
root.manager.current = 'camera_screen'

""")


# Declare both screens
class CameraScreen(Screen):
global camera_id # 选择摄像头
camera_id = 0
global preview_state # 控制预览是否开启
preview_state = True
global compensation # 曝光补偿
compensation = 1
# global camera
# camera = None

# global frame

def selectCamera(self, i):
"""选择摄像头"""
global camera_id
camera_id = i
print("selected camera " + str(camera_id))
# camera = self.index = camera_id
return camera_id

def selectCompensation(self, i):
"""响应设置的曝光补偿"""
global compensation
compensation = i
print("selected compensation " + str(compensation))
return compensation

def takePicture(self):
global compensation
global camera_id
global camera
pic_list = []
take_picture_number = 3
# self.ids['sjnCamera'].play = False
cam = cv2.VideoCapture(camera_id)
exposure_list = self.getExposureList(cam, compensation) # 获得曝光序列
cwd = os.getcwd()
while take_picture_number:
for t in exposure_list:
cam.set(cv2.CAP_PROP_EXPOSURE, t)
ret, frame_read = cam.read()
if ret:
time_str = time.strftime("%H%M%S")
picture_name = "IMG_{}".format(time_str) + "_" + str(take_picture_number) + ".jpg"
save_path = cwd + "/LDR/" + picture_name
cv2.imwrite(save_path, frame_read)
pic_list.append(picture_name) # 图片名称序列
print('保存图像成功')
print("camera_id:" + str(camera_id))
take_picture_number -= 1
# cv2.waitKey(1) # 延时1ms
else:
print("获取失败")
break
cam.release()
cam.set(cv2.CAP_PROP_AUTO_EXPOSURE, 0.75) # 设置回自动曝光

file_name = self.writeList(cwd, pic_list, exposure_list) # 写入txt
self.createHDR(cwd, file_name, time_str) # 生成HDR


# def createCamera(self):
# global camera_id
# global camera
# camera = cv2.VideoCapture(camera_id)
# # 调用preview的start,和update
# #self.ids.qrcam.start(camera) # 调用preview类的start,获取预览
# return camera

def getExposureList(self, cam, compensation):
exposure_list = []
auto_exposure_time = 1 / float(cv2.CAP_PROP_EXPOSURE) # 获取标准曝光时间
cam.set(cv2.CAP_PROP_AUTO_EXPOSURE, 0.25) # 切换为手动曝光
if compensation == 2:
exposure_time_m = auto_exposure_time / 4
exposure_time_p = auto_exposure_time * 4
exposure_list.append(exposure_time_m) # 曝光时间序列
exposure_list.append(auto_exposure_time)
exposure_list.append(exposure_time_p)
elif compensation == 3:
exposure_time_m = auto_exposure_time / 8
exposure_time_p = auto_exposure_time * 8
exposure_list.append(exposure_time_m) # 曝光时间序列
exposure_list.append(auto_exposure_time)
exposure_list.append(exposure_time_p)
else:
exposure_time_m = auto_exposure_time / 2
exposure_time_p = auto_exposure_time * 2
exposure_list.append(exposure_time_m) # 曝光时间序列
exposure_list.append(auto_exposure_time)
exposure_list.append(exposure_time_p)
return exposure_list

def loadExposureSeq(self, path, file_name):
"""载入曝光序列"""
images = []
times = []
list_path = path+"/List/"
LDR_path = path+"/LDR/"
file_path = os.path.join(list_path, file_name)
with open(file_path) as f: # 打开txt文件,按行读入到content
content = f.readlines()
for line in content: # 按行读取
tokens = line.split() # 存为数组
images.append(cv.imread(os.path.join(LDR_path, tokens[0])))
times.append(1 / float(tokens[1]))
return images, np.asarray(times, dtype=np.float32)

def writeList(self, path, frame_list, exposure_list):
path = path+"/List/"
list_name_index = time.strftime("%H%M%S")
file = list_name_index + '.txt'
i = len(frame_list)
f_path = os.path.join(path, file)
with open(os.path.join(path, file), 'a') as f:
while i:
f.write(str(frame_list[3 - i]) + ' ' + str(exposure_list[3 - i]) + '\n')
i -= 1
print("writeList 成功 ---")
return file

def createHDR(self, cwd, file_name, time_str):
images, times = self.loadExposureSeq(cwd, file_name) # 读取图形名称和曝光时间
alignMTB = cv2.createAlignMTB() # 创建中值阈值位图
alignMTB.process(images, images)
calibrate = cv.createCalibrateDebevec() # 获得响应曲线
response = calibrate.process(images, times)
merge_debevec = cv.createMergeDebevec() # 合并图像
hdr = merge_debevec.process(images, times, response)
tonemap = cv.createTonemap(2.2) # 映射
ldr = tonemap.process(hdr)
merge_mertens = cv.createMergeMertens()
fusion = merge_mertens.process(images)
fusion_picture_name = "fusion_{}".format(time_str) + ".png"
save_path = cwd+"/HDR/"+fusion_picture_name
cv.imwrite(save_path, fusion * 255)
print("HDR创建成功---")

file = "HDR_list" + '.txt' # 20210522 -add 每生成一张HDR图片,就在HDR_list中插入名字
HDR_list_path = cwd+"/HDR/"+fil
...全文
104 点赞 收藏 回复
写回复
回复

还没有回复,快来抢沙发~

发动态
发帖子
Android
创建于2009-10-09

4.6w+

社区成员

9.0w+

社区内容

移动平台 Android
社区公告
暂无公告