capture_basic_mode.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. import time
  2. import settings
  3. from sys import platform as sys_platform
  4. from utils.utils_func import httpGetHandler
  5. from PIL import Image
  6. from io import BytesIO
  7. import requests
  8. if settings.IS_TEST:
  9. import piexif
  10. from PIL import Image
  11. import threading
  12. class CaptureBasic():
  13. # program_sign = Signal(dict)0
  14. base_host = "http://localhost:5513"
  15. _get_image_index = 0
  16. _get_folder_index = 0
  17. is_first_init = 0
  18. def __init__(self):
  19. # self.window = window
  20. # 状态 1 进行中 2 已停止
  21. self.state = 2
  22. # 总拍照数量
  23. self.total_num = 0
  24. # 是否获取照片
  25. self.is_get_photo = False
  26. # self.keyboard = Controller()
  27. self.hwnd = None
  28. # def getPreviewImage(self)->Image:
  29. # '''获取预览图'''
  30. # yzmdata = requests.get(self.base_host + "/preview.jpg")
  31. # tempIm = BytesIO(yzmdata.content)
  32. # im = Image.open(tempIm)
  33. # return im
  34. def auto_focus(self):
  35. # 执行拍照
  36. time.sleep(0.2)
  37. self.run_capture_action("LiveView_Focus")
  38. def getCaptureFolderPath(self):
  39. """获取预览图"""
  40. response = httpGetHandler(
  41. url=self.base_host,
  42. params={"slc": "get", "param1": "session.folder"},
  43. headers={},
  44. )
  45. return response.text
  46. def checkCameraConnect(self):
  47. """检查相机是否连接"""
  48. response = httpGetHandler(
  49. url=self.base_host,
  50. params={"slc": "get", "param1": "iso"},
  51. headers={},
  52. )
  53. if response.text == "未将对象引用设置到对象的实例。":
  54. return False
  55. return True
  56. def run_capture_action(self,command):
  57. '''执行capture动作'''
  58. response = httpGetHandler(url=self.base_host, params={"CMD":command},headers={})
  59. return response
  60. # def photograph(self):
  61. # """
  62. # Args:
  63. # data:
  64. # info_show: 信息输出
  65. # image_deal_mode:0 表示左脚,1表示右脚
  66. # Returns:
  67. # """
  68. # print("142----------------------------photograph")
  69. # # 执行拍照
  70. # if not self.hwnd:
  71. # return False, {"flag": "{} 未打开".format(settings.CaptureSoftwareName)}
  72. # if self.state == 1:
  73. # print("当前有未完成的任务")
  74. # return False, {"flag": "当前有未完成的任务"}
  75. # print("开始拍照")
  76. # self.state = 1
  77. # if not self.set_foreground_window():
  78. # return False, {"flag": "no {}".format(settings.CaptureSoftwareName)}
  79. # time.sleep(0.1)
  80. # # 自动对焦
  81. # self.auto_focus()
  82. # # self.send_win32_key(self.hwnd, key_1=win32con.VK_CONTROL, key_2=win32con.VK_F2)
  83. # if settings.IS_TEST:
  84. # # print("56-----测试拍照")
  85. # threading.Thread(target=self.test_add_photo, args=()).start()
  86. # # self.program_sign.emit({"type": "add_to_photo_todo_list",
  87. # # "data": data})
  88. # time.sleep(float(settings.PhotographSeconds))
  89. # def test_add_photo(self):
  90. # def copy_file(srcfile, dstpath, dstfile): # 复制函数
  91. # if not os.path.isfile(srcfile):
  92. # # print("%s not exist!" % (srcfile))
  93. # return
  94. # else:
  95. # if int(os.path.getsize(srcfile) / 1024) < 10:
  96. # return
  97. # # fpath, fname = os.path.split(srcfile) # 分离文件名和路径
  98. # if not os.path.exists(dstpath):
  99. # os.makedirs(dstpath) # 创建路径
  100. # shutil.copy(srcfile, dstfile) # 复制文件
  101. # print("模拟拍照 test_add_photo")
  102. # time.sleep(0.9)
  103. # s = time.time()
  104. # source_path = r"{}\PhotoImageDir\test_source".format(os.getcwd())
  105. # check_path(source_path)
  106. # # 整理所有测试源照片
  107. # images_path_list = []
  108. # folder_list = get_folder(source_path)
  109. # for folder_dict in folder_list:
  110. # folder_path = folder_dict["folder_path"]
  111. # _pics = [x["file_path"] for x in get_images(folder_path)]
  112. # if _pics:
  113. # images_path_list.append(_pics)
  114. # if len(images_path_list) <= CaptureBasic._get_folder_index:
  115. # CaptureBasic._get_folder_index = 0
  116. # CaptureBasic._get_image_index = 0
  117. # srcfile = images_path_list[CaptureBasic._get_folder_index][CaptureBasic._get_image_index]
  118. # # 新文件夹
  119. # CaptureBasic._get_image_index += 1
  120. # if len(images_path_list[CaptureBasic._get_folder_index]) <= CaptureBasic._get_image_index:
  121. # CaptureBasic._get_folder_index += 1
  122. # CaptureBasic._get_image_index = 0
  123. # # print("pic:", srcfile)
  124. # e = os.path.splitext(os.path.split(srcfile)[1])[1]
  125. # # 修改图片时间
  126. # if e != ".CR2":
  127. # _time = time.strftime("%Y:%m:%d %H:%M:%S", time.localtime(time.time()))
  128. # exif_ifd = {piexif.ExifIFD.DateTimeOriginal: _time}
  129. # exif_dict = {"Exif": exif_ifd}
  130. # exif_bytes = piexif.dump(exif_dict)
  131. # im = Image.open(srcfile)
  132. # im.save(srcfile, exif=exif_bytes)
  133. # dst_path = settings.PhotoOutputDir
  134. # # dst_path = "{}/data".format(os.getcwd())
  135. # e = os.path.splitext(srcfile)[-1]
  136. # new_name = "~{}{}".format(int(time.time() * 10), e)
  137. # dst_file = "{}/{}".format(dst_path, new_name)
  138. # print("测试数据 dst_file", dst_file)
  139. # copy_file(srcfile, dst_path, dst_file)
  140. # # print("修改图片耗时:{}".format(time.time() - s))
  141. def last_images(self):
  142. if not self.hwnd:
  143. return
  144. # win32gui.SetForegroundWindow(self.hwnd)
  145. # self.keyboard.press(Key.ctrl)
  146. # self.keyboard.press(Key.left)
  147. # self.keyboard.release(Key.left)
  148. # self.keyboard.release(Key.ctrl)
  149. def next_images(self):
  150. # 执行拍照
  151. if not self.hwnd:
  152. return
  153. # win32gui.SetForegroundWindow(self.hwnd)
  154. # self.keyboard.press(Key.ctrl)
  155. # self.keyboard.press(Key.right)
  156. # self.keyboard.release(Key.right)
  157. # self.keyboard.release(Key.ctrl)
  158. def test_1(self):
  159. if not self.hwnd:
  160. return
  161. # 执行照片全选与导出
  162. # win32gui.SetForegroundWindow(self.hwnd)
  163. # self.keyboard.press(Key.ctrl)
  164. # self.keyboard.press("a")
  165. # self.keyboard.release("a")
  166. # self.keyboard.release(Key.ctrl)
  167. # time.sleep(1)
  168. # # 照片导出
  169. # self.keyboard.press(Key.ctrl)
  170. # self.keyboard.press("d")
  171. # self.keyboard.release("d")
  172. # self.keyboard.release(Key.ctrl)
  173. if __name__ == "__main__":
  174. cap = CaptureBasic()
  175. # cap.auto_focus()
  176. # cap.auto_focus()
  177. # cap.run_capture_action("Capture")
  178. camera_conn = cap.checkCameraConnect()
  179. print("cccc", camera_conn)