【多功能改进】基于OpenCV图像采集的人脸识别网络推流及局域网无线控制系统(将图像在URL地址上输出,可做成网络摄像头,带识别框)
人脸识别和网络推流
前文:
blog.csdn.net/weixin_53403301/article/details/124096787
资源:
download.csdn.net/download/weixin_53403301/85142311
在运行项目前,先在当前目录下新建目录templates
在目录内新建index.html文件 并输入HTML代码:
<html>
<!--meta http-equiv="refresh" content="5"-->
<head>
<title>智能导播系统</title>
</head>
<body>
<h1>智能导播系统</h1>
<form action="/" method="post" style="float:left">
<p>
<input type="submit" style="font-size:50px" name="auto" value="自动模式">
<input type="submit" style="font-size:50px" name="manu" value="手动模式">
</p>
<p>
<input type="submit" style="font-size:50px" name="reset" value="复位导轨">
<input type="submit" style="font-size:50px" name="set" value="设置点位">
</p>
<p>
<input type="submit" style="font-size:50px" name="left" value="左移导轨">
<input type="submit" style="font-size:50px" name="right" value="右移导轨">
</p>
<p>
<input type="submit" style="font-size:50px" name="go_in" value="继续程序">
<input type="submit" style="font-size:50px" name="go_out" value="暂停程序">
</p>
<p>
<input type="submit" style="font-size:50px" name="stop" value="停止导轨">
<input type="submit" style="font-size:50px" name="quit" value="全部退出">
</p>
<p>
<input type="submit" style="font-size:50px" name="left_step" value="左移一步">
<input type="submit" style="font-size:50px" name="right_step" value="右移一步">
</p>
</form>
<img src="{{ url_for('video_feed') }}" height="540" style="float:left">
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<h2>手势及功能说明</h2>
<h4 style="color:red">起始手势——左/右手“5”:开启手势控制模式,其他手势控制功能仅在开启该模式时有效,若10秒内无任一手势控制操作,则需手势“5"解锁;五指全部打开。</h4>
<h4 style="color:blue">自动模式——右手“Spider-Man”:该模式下自动识别人脸并追踪,手动模式操作不可用;打开拇指、食指、小指,闭合中指、无名指。</h4>
<h4 style="color:green">手动模式——左手“Rock'n'Roll”:该模式下不进行识别人脸或追踪,手动模式操作可用;打开食指、小指,闭合拇指、中指、无名指。</h4>
<h4 style="color:blue">复位导轨——右手“Thumb Up”:手动模式操作。使导轨复位到标记的初始位置;竖起拇指,其他闭合。</h4>
<h4 style="color:blue">设置点位——右手“Princess”:手动模式操作。标记导轨初始位置;打开中指、无名指、小指,闭合拇指、食指,又为“OK”手势。</h4>
<h4 style="color:green">左移导轨——左手“2”:手动模式操作。左移导轨;竖起食指、中指,其他闭合。</h4>
<h4 style="color:green">右移导轨——左手“1”:手动模式操作。右移导轨;竖起食指,其他闭合。</h4>
<h4 style="color:blue">继续程序——右手“8”:恢复暂停的程序及功能;竖起拇指、食指,其他闭合。</h4>
<h4 style="color:blue">暂停程序——右手“6”:暂停图像、识别、运动、手势功能,仅保留网页端控制功能;竖起拇指、小指,其他闭合。</h4>
<h4 style="color:green">停止导轨——左手“0”:手动模式操作。停止导轨;全部闭合,握拳。</h4>
<h4 style="color:red">全部退出——左/右手“Bye”:保持三秒即可退出所有程序,退出后无法执行任何功能;竖起拇指、食指、中指,其他闭合。</h4>
<h4 style="color:green">左移一步——左手“4”:手动模式操作。左移一步;拇指闭合,其他打开。</h4>
<h4 style="color:green">右移一步——左手“3”:手动模式操作。右移一步;拇指、小指闭合,其他打开。</h4>
</body>
</html>
网页效果:
首先建立服务器,同时用OpenCV调用本地摄像头采集图像 并转为JPEG格式 而后进行推流
同时监控表单输入,使其能够执行其他函数
服务器建立成功后,即可在浏览器输入IP地址和端口进行查看
在浏览器中输入
http://A.B.C.D:yyyy/
打开即可
其中A.B.C.D为服务器IP地址 yyyy为端口号 这里设置的为1212
代码如下:
import cv2
from flask import Flask, render_template, Response, request
import threading
import socket
import mediapipe as mp
import math
import time
global q
q = 0
global kill_all_flag
kill_all_flag = 0
global model_flag
model_flag = 0
global command_str
command_str = None
local_post = 1212
s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
s.connect(("8.8.8.8",80))
local_ip = str(s.getsockname()[0])
s.close()
app = Flask(__name__)
cap = cv2.VideoCapture(0) # 开启摄像头
classifier = cv2.CascadeClassifier('haarcascade_frontalface_alt2.xml')
global cam_img
global faceImg
ok, cam_img = cap.read() # 读取摄像头图像
if ok is False:
q = 1
kill_all_flag = 1
print('无法读取到摄像头!')
faceImg = cam_img
high=faceImg.shape[0]
width=faceImg.shape[1]
left_point = width/2+25
right_point = width/2-25
mp_drawing = mp.solutions.drawing_utils
mp_hands = mp.solutions.hands
hands = mp_hands.Hands(static_image_mode=False,max_num_hands=1,min_detection_confidence=0.75,min_tracking_confidence=0.75)
def vector_2d_angle(v1,v2):
'''
求解二维向量的角度
'''
v1_x=v1[0]
v1_y=v1[1]
v2_x=v2[0]
v2_y=v2[1]
try:
angle_= math.degrees(math.acos((v1_x*v2_x+v1_y*v2_y)/(((v1_x**2+v1_y**2)**0.5)*((v2_x**2+v2_y**2)**0.5))))
except:
angle_ =65535.
if angle_ > 180.:
angle_ = 65535.
return angle_
def hand_angle(hand_):
'''
获取对应手相关向量的二维角度,根据角度确定手势
'''
angle_list = []
#---------------------------- thumb 大拇指角度
angle_ = vector_2d_angle(
((int(hand_[0][0])- int(hand_[2][0])),(int(hand_[0][1])-int(hand_[2][1]))),
((int(hand_[3][0])- int(hand_[4][0])),(int(hand_[3][1])- int(hand_[4][1])))
)
angle_list.append(angle_)
#---------------------------- index 食指角度
angle_ = vector_2d_angle(
((int(hand_[0][0])-int(hand_[6][0])),(int(hand_[0][1])- int(hand_[6][1]))),
((int(hand_[7][0])- int(hand_[8][0])),(int(hand_[7][1])- int(hand_[8][1])))
)
angle_list.append(angle_)
#---------------------------- middle 中指角度
angle_ = vector_2d_angle(
((int(hand_[0][0])- int(hand_[10][0])),(int(hand_[0][1])- int(hand_[10][1]))),
((int(hand_[11][0])- int(hand_[12][0])),(int(hand_[11][1])- int(hand_[12][1])))
)
angle_list.append(angle_)
#---------------------------- ring 无名指角度
angle_ = vector_2d_angle(
((int(hand_[0][0])- int(hand_[14][0])),(int(hand_[0][1])- int(hand_[14][1]))),
((int(hand_[15][0])- int(hand_[16][0])),(int(hand_[15][1])- int(hand_[16][1])))
)
angle_list.append(angle_)
#---------------------------- pink 小拇指角度
angle_ = vector_2d_angle(
((int(hand_[0][0])- int(hand_[18][0])),(int(hand_[0][1])- int(hand_[18][1]))),
((int(hand_[19][0])- int(hand_[20][0])),(int(hand_[19][1])- int(hand_[20][1])))
)
angle_list.append(angle_)
return angle_list
def h_gesture(angle_list):
'''
# 二维约束的方法定义手势
# fist five gun love one six three thumbup yeah
'''
thr_angle = 65. #手指闭合则大于这个值(大拇指除外)
thr_angle_thumb = 53. #大拇指闭合则大于这个值
thr_angle_s = 49. #手指张开则小于这个值
gesture_str = "Unknown"
if 65535. not in angle_list:
if (angle_list[0]>thr_angle_thumb) and (angle_list[1]>thr_angle) and (angle_list[2]>thr_angle) and (angle_list[3]>thr_angle) and (angle_list[4]>thr_angle):
gesture_str = "0"
elif (angle_list[0]>thr_angle_thumb) and (angle_list[1]<thr_angle_s) and (angle_list[2]>thr_angle) and (angle_list[3]>thr_angle) and (angle_list[4]>thr_angle):
gesture_str = "1"
elif (angle_list[0]>thr_angle_thumb) and (angle_list[1]<thr_angle_s) and (angle_list[2]<thr_angle_s) and (angle_list[3]>thr_angle) and (angle_list[4]>thr_angle):
gesture_str = "2"
elif (angle_list[0]>thr_angle_thumb) and (angle_list[1]<thr_angle_s) and (angle_list[2]<thr_angle_s) and (angle_list[3]<thr_angle_s) and (angle_list[4]>thr_angle):
gesture_str = "3"
elif (angle_list[0]>thr_angle_thumb) and (angle_list[1]<thr_angle_s) and (angle_list[2]<thr_angle_s) and (angle_list[3]<thr_angle_s) and (angle_list[4]<thr_angle_s):
gesture_str = "4"
elif (angle_list[0]<thr_angle_s) and (angle_list[1]<thr_angle_s) and (angle_list[2]<thr_angle_s) and (angle_list[3]<thr_angle_s) and (angle_list[4]<thr_angle_s):
gesture_str = "5"
elif (angle_list[0]<thr_angle_s) and (angle_list[1]>thr_angle) and (angle_list[2]>thr_angle) and (angle_list[3]>thr_angle) and (angle_list[4]<thr_angle_s):
gesture_str = "6"
elif (angle_list[0]<thr_angle_s) and (angle_list[1]<thr_angle_s) and (angle_list[2]>thr_angle) and (angle_list[3]>thr_angle) and (angle_list[4]>thr_angle):
gesture_str = "8"
elif (angle_list[0]>thr_angle_thumb) and (angle_list[1]>thr_angle) and (angle_list[2]>thr_angle) and (angle_list[3]>thr_angle) and (angle_list[4]<thr_angle_s):
gesture_str = "Pink Up"
elif (angle_list[0]<thr_angle_s) and (angle_list[1]>thr_angle) and (angle_list[2]>thr_angle) and (angle_list[3]>thr_angle) and (angle_list[4]>thr_angle):
gesture_str = "Thumb Up"
elif (angle_list[0]>thr_angle_thumb) and (angle_list[1]>thr_angle) and (angle_list[2]<thr_angle_s) and (angle_list[3]>thr_angle) and (angle_list[4]>thr_angle):
gesture_str = "Fuck"
elif (angle_list[0]>thr_angle_thumb) and (angle_list[1]>thr_angle) and (angle_list[2]<thr_angle_s) and (angle_list[3]<thr_angle_s) and (angle_list[4]<thr_angle_s):
gesture_str = "Princess"
elif (angle_list[0]<thr_angle_s) and (angle_list[1]<thr_angle_s) and (angle_list[2]<thr_angle_s) and (angle_list[3]>thr_angle) and (angle_list[4]>thr_angle):
gesture_str = "Bye"
elif (angle_list[0]<thr_angle_s) and (angle_list[1]<thr_angle_s) and (angle_list[2]>thr_angle) and (angle_list[3]>thr_angle) and (angle_list[4]<thr_angle_s):
gesture_str = "Spider-Man"
elif (angle_list[0]>thr_angle_thumb) and (angle_list[1]<thr_angle_s) and (angle_list[2]>thr_angle) and (angle_list[3]>thr_angle) and (angle_list[4]<thr_angle_s):
gesture_str = "Rock'n'Roll"
return gesture_str
def command_jugg():
global kill_all_flag
global q
global command_list
global model_flag
for i in command_list:
if i != "None":
command_str = i
break
print(command_str)
if command_str == "暂停程序":
q = 1
print("Pause")
elif command_str == "继续程序":
q = 0
print("Continue")
elif command_str == "全部退出":
kill_all_flag = 1
q = 1
print("Good-Bye")
elif command_str == "自动模式":
model_flag = 1
print("Automatic")
elif command_str == "手动模式":
model_flag = 0
print("Manual")
if model_flag == 0:
if command_str == "复位导轨":
print("Reset")
elif command_str == "设置点位":
print("Set")
elif command_str == "左移导轨":
print("Left")
elif command_str == "右移导轨":
print("Right")
elif command_str == "停止导轨":
print("Stop")
command_str = None
@app.route('/', methods=['GET', 'POST'])
def index():
global command_list
global kill_all_flag
global q
global command_str
if request.method == 'POST':
command_list = []
c0 = str(request.form.get('auto'))
c1 = str(request.form.get('manu'))
c2 = str(request.form.get('left'))
c3 = str(request.form.get('right'))
c4 = str(request.form.get('stop'))
c5 = str(request.form.get('reset'))
c6 = str(request.form.get('set'))
c7 = str(request.form.get('quit'))
c8 = str(request.form.get('go_in'))
c9 = str(request.form.get('go_out'))
command_list = [c0,c1,c2,c3,c4,c5,c6,c7,c8,c9]
if kill_all_flag == 0 and q == 0:
command_jugg()
elif kill_all_flag == 0 and q == 1:
for i in command_list:
if i != "None":
command_str = i
break
print(command_str)
if command_str == "继续程序":
q = 0
print("Continue")
else:
print("程序已暂停,继续请按键")
else:
print("程序已终止,请重启程序")
return render_template('index.html')
def hand_detect():
global faceImg
global q
global kill_all_flag
global cam_img
global hand_jugg_flag
global hand_str_flag
global hand_jugg
global gesture_str
# global hand_landmarks
# global draw_flag
global model_flag
detect_flag = 0
detect_time = time.time()
bye_flag = 0
bye_time = time.time()
hand_jugg = None
gesture_str = None
while True:
time.sleep(0.1)
while q==0:
time.sleep(0.1)
hand_jugg_flag = 0
hand_str_flag = 0
# draw_flag = 0
frame = cv2.cvtColor(cam_img, cv2.COLOR_BGR2RGB)
results = hands.process(frame)
if results.multi_handedness:
for hand_label in results.multi_handedness:
hand_jugg=str(hand_label).split('"')[1]+" Hand"
print(hand_jugg)
hand_jugg_flag = 1
# cv2.putText(faceImg,hand_jugg,(50,200),0,1.3,(0,0,255),2)
if results.multi_hand_landmarks:
for hand_landmarks in results.multi_hand_landmarks:
mp_drawing.draw_landmarks(faceImg, hand_landmarks, mp_hands.HAND_CONNECTIONS)
# draw_flag = 1
hand_local = []
for i in range(21):
x = hand_landmarks.landmark[i].x*frame.shape[1]
y = hand_landmarks.landmark[i].y*frame.shape[0]
hand_local.append((x,y))
if hand_local:
angle_list = hand_angle(hand_local)
gesture_str = h_gesture(angle_list)
print(gesture_str)
hand_str_flag = 1
# cv2.putText(faceImg,gesture_str,(50,100),0,1.3,(0,0,255),2)
if gesture_str == "5":
bye_flag = 0
detect_flag = 1
detect_time = time.time()
gesture_str = None
if detect_flag == 1:
if gesture_str == "Bye":
detect_time = time.time()
if bye_flag == 0:
bye_time = time.time()
bye_flag = 1
elif bye_flag == 1 and time.time() - bye_time >= 3:
kill_all_flag = 1
q = 1
print("Good-Bye")
else:
bye_flag = 1
elif hand_jugg == "Right Hand" and gesture_str == "Spider-Man":
detect_time = time.time()
bye_flag = 0
model_flag = 1
print("Automatic")
elif hand_jugg == "Left Hand" and gesture_str == "Rock'n'Roll":
detect_time = time.time()
bye_flag = 0
model_flag = 0
print("Manual")
elif hand_jugg == "Right Hand" and gesture_str == "6":
detect_time = time.time()
bye_flag = 0
q = 1
print("Pause")
elif hand_jugg == "Right Hand" and gesture_str == "8":
detect_time = time.time()
bye_flag = 0
q = 0
print("Continue")
else:
bye_flag = 0
if model_flag == 0:
if gesture_str == "Bye":
detect_time = time.time()
if bye_flag == 0:
bye_time = time.time()
bye_flag = 1
elif bye_flag == 1 and time.time() - bye_time >= 3:
kill_all_flag = 1
q = 1
print("Good-Bye")
else:
bye_flag = 1
elif hand_jugg == "Left Hand" and gesture_str == "1":
detect_time = time.time()
bye_flag = 0
print("Right")
elif hand_jugg == "Left Hand" and gesture_str == "2":
detect_time = time.time()
bye_flag = 0
print("Left")
elif hand_jugg == "Left Hand" and gesture_str == "0":
detect_time = time.time()
bye_flag = 0
print("Central")
elif hand_jugg == "Right Hand" and gesture_str == "Thumb Up":
detect_time = time.time()
bye_flag = 0
print("Reset")
elif hand_jugg == "Right Hand" and gesture_str == "Princess":
detect_time = time.time()
bye_flag = 0
print("Set")
else:
bye_flag = 0
hand_jugg = None
gesture_str = None
if time.time() - detect_time >= 10:
detect_flag = 0
if q == 1:
# STOP()
print("Stop")
break
if kill_all_flag == 1:
break
# STOP()
print("Stop")
return
def track():
global faceImg
global q
global kill_all_flag
global cam_img
global x,y,w,h
global track_flag
global model_flag
while True:
time.sleep(0.1)
while q==0:
time.sleep(0.1)
track_flag = 0
if model_flag == 1:
gray = cv2.cvtColor(cam_img,cv2.COLOR_BGR2GRAY)
faceRects = classifier.detectMultiScale(gray,scaleFactor=1.2,minNeighbors=3,minSize=(32, 32))
if len(faceRects):
track_flag = 1
x,y,w,h = faceRects[0]
# 框选出人脸 最后一个参数2是框线宽度
# cv2.rectangle(faceImg,(x, y), (x + w, y + h), (0,255,0), 2)
central_point = x+w/2
if central_point > left_point:
# LEFT()
print("Right")
elif central_point < right_point:
# RIGHT()
print("Left")
else:
# STOP()
print("Central")
if q == 1:
# STOP()
print("Stop")
break
if kill_all_flag == 1:
break
# STOP()
print("Stop")
return
def draw():
global faceImg
global q
global kill_all_flag
# global cam_img
global x,y,w,h
global track_flag
global hand_jugg_flag
global hand_str_flag
global hand_jugg
global gesture_str
# global hand_landmarks
# global draw_flag
while True:
time.sleep(0.1)
while q==0:
time.sleep(0.1)
# faceImg = cam_img
# if draw_flag == 1:
# mp_drawing.draw_landmarks(faceImg, hand_landmarks, mp_hands.HAND_CONNECTIONS)
if track_flag == 1:
cv2.rectangle(faceImg,(x, y), (x + w, y + h), (0,255,0), 2)
if hand_str_flag == 1:
cv2.putText(faceImg,gesture_str,(50,100),0,1.3,(0,0,255),2)
if hand_jugg_flag == 1:
cv2.putText(faceImg,hand_jugg,(50,200),0,1.3,(0,0,255),2)
if q == 1: # 通过esc键退出摄像
q = 1
# STOP()
print("Stop")
break
if kill_all_flag == 1:
break
def img_main():
global faceImg
global q
global kill_all_flag
global cam_img
global x,y,w,h
global track_flag
global hand_jugg_flag
global hand_str_flag
global hand_jugg
global gesture_str
# global hand_landmarks
# global draw_flag
hand_jugg = None
x,y,w,h = None,None,None,None
gesture_str = None
track_flag = 0
hand_str_flag = 0
hand_jugg_flag = 0
thread_track = threading.Thread(target=track)
thread_track.setDaemon(True)
thread_track.start()
thread_hand = threading.Thread(target=hand_detect)
thread_hand.setDaemon(True)
thread_hand.start()
# thread_draw = threading.Thread(target=draw)
# thread_draw.setDaemon(True)
# thread_draw.start()
while True:
time.sleep(0.1)
while q==0:
cam_img = cv2.flip(cap.read()[1],1)
faceImg = cam_img
cv2.imshow("http://"+local_ip+":"+str(local_post)+"/ (img: video_feed)",faceImg)
# 展示图像
if q == 1: # 通过esc键退出摄像
q = 1
# STOP()
print("Stop")
print("暂停程序")
cv2.destroyAllWindows()
break
if cv2.waitKey(10) == 27:
kill_all_flag = 1
q = 1
print("Stop")
print("结束程序")
cv2.destroyAllWindows()
break
if kill_all_flag == 1:
break
cap.release()
# STOP()
print("Stop")
print("全部退出")
return
def send_img():
global faceImg
global q
while q == 0:
image = cv2.imencode('.jpg', faceImg)[1].tobytes()
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + image + b'\r\n')
if q == 1:
break
return
@app.route('/video_feed')
def video_feed():
return Response(send_img(), mimetype='multipart/x-mixed-replace; boundary=frame')
def start_server():
app.run(host='0.0.0.0', port=local_post)
def main():
thread_send = threading.Thread(target=start_server)
thread_send.setDaemon(True)
thread_send.start()
img_main()
time.sleep(1)
print("Stop")
print("已退出所有程序")
return
if __name__ == "__main__":
main()
效果如下:
手机端效果:
附录:列表的赋值类型和py打包
列表赋值
BUG复现
闲来无事写了个小程序 代码如下:
# -*- coding: utf-8 -*-
"""
Created on Fri Nov 19 19:47:01 2021
@author: 16016
"""
a_list = ['0','1','2','3','4','5','6','7','8','9','10','11','12','13','14','15']
#print(len(a_list))
#b_list = ['','','','','','','','','','','','','','','','']
c_list = [[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]]
#for i in range(16):
if len(a_list):
for j in range(16):
a_list[j]=str(a_list[j])+'_'+str(j)
print("序号:",j)
print('a_list:\n',a_list)
c_list[j]=a_list
print('c_list[0]:\n',c_list[0])
print('\n')
# b_list[j]=a_list[7],a_list[8]
# print(b_list[j])
# 写入到Excel:
#print(c_list,'\n')
我在程序中 做了一个16次的for循环 把列表a的每个值后面依次加上"_"和循环序号
比如循环第x次 就是把第x位加上_x 这一位变成x_x 我在输出测试中 列表a的每一次输出也是对的
循环16次后列表a应该变成[‘0_0’, ‘1_1’, ‘2_2’, ‘3_3’, ‘4_4’, ‘5_5’, ‘6_6’, ‘7_7’, ‘8_8’, ‘9_9’, ‘10_10’, ‘11_11’, ‘12_12’, ‘13_13’, ‘14_14’, ‘15_15’] 这也是对的
同时 我将每一次循环时列表a的值 写入到空列表c中 比如第x次循环 就是把更改以后的列表a的值 写入到列表c的第x位
第0次循环后 c[0]的值应该是[‘0_0’, ‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’, ‘10’, ‘11’, ‘12’, ‘13’, ‘14’, ‘15’] 这也是对的
但是在第1次循环以后 c[0]的值就一直在变 变成了c[x]的值
相当于把c_list[0]变成了c_list[1]…以此类推 最后得出的列表c的值也是每一项完全一样
我不明白这是怎么回事
我的c[0]只在第0次循环时被赋值了 但是后面它的值跟着在改变
如图:
第一次老出bug 赋值以后 每次循环都改变c[0]的值 搞了半天都没搞出来
无论是用appen函数添加 还是用二维数组定义 或者增加第三个空数组来过渡 都无法解决
代码改进
后来在我华科同学的指导下 突然想到赋值可以赋的是个地址 地址里面的值一直变化 导致赋值也一直变化 于是用第二张图的循环套循环深度复制实现了
代码如下:
# -*- coding: utf-8 -*-
"""
Created on Fri Nov 19 19:47:01 2021
@author: 16016
"""
a_list = ['0','1','2','3','4','5','6','7','8','9','10','11','12','13','14','15']
#print(len(a_list))
#b_list = ['','','','','','','','','','','','','','','','']
c_list = [[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]]
#for i in range(16):
if len(a_list):
for j in range(16):
a_list[j]=str(a_list[j])+'_'+str(j)
print("序号:",j)
print('a_list:\n',a_list)
for i in range(16):
c_list[j].append(a_list[i])
print('c_list[0]:\n',c_list[0])
print('\n')
# b_list[j]=a_list[7],a_list[8]
# print(b_list[j])
# 写入到Excel:
print(c_list,'\n')
解决了问题
优化
第三次是请教了老师 用copy函数来赋真值
代码如下:
# -*- coding: utf-8 -*-
"""
Created on Fri Nov 19 19:47:01 2021
@author: 16016
"""
a_list = ['0','1','2','3','4','5','6','7','8','9','10','11','12','13','14','15']
#print(len(a_list))
#b_list = ['','','','','','','','','','','','','','','','']
c_list = [[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]]
#for i in range(16):
if len(a_list):
for j in range(16):
a_list[j]=str(a_list[j])+'_'+str(j)
print("序号:",j)
print('a_list:\n',a_list)
c_list[j]=a_list.copy()
print('c_list[0]:\n',c_list[0])
print('\n')
# b_list[j]=a_list[7],a_list[8]
# print(b_list[j])
# 写入到Excel:
#print(c_list,'\n')
同样能解决问题
最后得出问题 就是指针惹的祸!
a_list指向的是个地址 而不是值 a_list[i]指向的才是单个的值 copy()函数也是复制值而不是地址
如果这个用C语言来写 就直观一些了 难怪C语言是基础 光学Python不学C 遇到这样的问题就解决不了
C语言yyds Python是什么垃圾弱智语言
总结
由于Python无法单独定义一个值为指针或者独立的值 所以只能用列表来传送
只要赋值是指向一个列表整体的 那么就是指向的一个指针内存地址 解决方法只有一个 那就是将每个值深度复制赋值(子列表内的元素提取出来重新依次连接) 或者用copy函数单独赋值
如图测试:
部分代码:
# -*- coding: utf-8 -*-
"""
Created on Sat Nov 20 16:45:48 2021
@author: 16016
"""
def text1():
A=[1,2,3]
B=[[],[],[]]
for i in range(len(A)):
A[i]=A[i]+i
B[i]=A
print(B)
def text2():
A=[1,2,3]
B=[[],[],[]]
A[0]=A[0]+0
B[0]=A
print(B)
A[1]=A[1]+1
B[1]=A
print(B)
A[2]=A[2]+2
B[2]=A
print(B)
if __name__ == '__main__':
text1()
print('\n')
text2()
py打包
Pyinstaller打包exe(包括打包资源文件 绝不出错版)
依赖包及其对应的版本号
PyQt5 5.10.1
PyQt5-Qt5 5.15.2
PyQt5-sip 12.9.0
pyinstaller 4.5.1
pyinstaller-hooks-contrib 2021.3
Pyinstaller -F setup.py 打包exe
Pyinstaller -F -w setup.py 不带控制台的打包
Pyinstaller -F -i xx.ico setup.py 打包指定exe图标打包
打包exe参数说明:
-F:打包后只生成单个exe格式文件;
-D:默认选项,创建一个目录,包含exe文件以及大量依赖文件;
-c:默认选项,使用控制台(就是类似cmd的黑框);
-w:不使用控制台;
-p:添加搜索路径,让其找到对应的库;
-i:改变生成程序的icon图标。
如果要打包资源文件
则需要对代码中的路径进行转换处理
另外要注意的是 如果要打包资源文件 则py程序里面的路径要从./xxx/yy换成xxx/yy 并且进行路径转换
但如果不打包资源文件的话 最好路径还是用作./xxx/yy 并且不进行路径转换
def get_resource_path(relative_path):
if hasattr(sys, '_MEIPASS'):
return os.path.join(sys._MEIPASS, relative_path)
return os.path.join(os.path.abspath("."), relative_path)
而后再spec文件中的datas部分加入目录
如:
a = Analysis(['cxk.py'],
pathex=['D:\\Python Test\\cxk'],
binaries=[],
datas=[('root','root')],
hiddenimports=[],
hookspath=[],
hooksconfig={},
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher,
noarchive=False)
而后直接Pyinstaller -F setup.spec即可
如果打包的文件过大则更改spec文件中的excludes 把不需要的库写进去(但是已经在环境中安装了的)就行
这些不要了的库在上一次编译时的shell里面输出
比如:
然后用pyinstaller --clean -F 某某.spec