Specify where to save image taken with webcame Python - python-3.x

So I have a Python application that accesses the built-in webcam on a laptop and takes a picture. But I'm having difficulty specifying the storage location for the picture (in this case on the desktop). The code I have so far is:
import cv2
import time
import getpass
import os
getUser = getpass.getuser()
save = 'C:/Users/' + getUser + "/Desktop"
camera_port = 0
camera = cv2.VideoCapture(camera_port)
time.sleep(0.1)
return_value, image = camera.read()
os.path.join(cv2.imwrite(save, "user.png", image))
del camera
But when I run it I get the following error:
Traceback (most recent call last):
File "C:/Users/RedCode/PycharmProjects/MyApps/WebcamPic.py", line 13, in <module>
os.path.join(cv2.imwrite(save, "user.png", image))
TypeError: img is not a numpy array, neither a scalar
How can I specify where to store the image when it is taken?

This line here is where you have a problem.
os.path.join(cv2.imwrite(save, "user.png", image))
You want to do this
cv2.imwrite(os.path.join(save, "user.png"), image)
imwrite expects two arguments the file name and the image to be saved.
The call to os.path.join is building your saved file path.

Related

Converting .wav audio files to .h5 (hdf) files using SciPy and PyTables

I need to convert audio .wav files to the .hf or the .npz format, as they are the supported format for training speech translation systems with FBK-Fairseq-ST (https://github.com/mattiadg/FBK-Fairseq-ST).
The following script is meant to run from terminal as python script.py /path/file.wav and write a new hdf file storing the information of the .wav file in the same folder.
from scipy.io import wavfile
import tables
import numpy
import sys
#read data from wav
#fs, data = wavfile.read('/home/vittoria/Documents/corpus-test/01.wav')
fs, data = wavfile.read(sys.argv[1])
#ouput
folder=sys.argv[1][:-6]
name= sys.argv[1][-6:-3]+"h5"
#save_to acoular h5 format
acoularh5 = tables.open_file(folder+name, mode = "w", title = name)
acoularh5.create_earray('/','time_data', atom=None, title='', filters=None, \
expectedrows=100000, chunkshape=[256,64], \
byteorder=None, createparents=False, obj=data)
acoularh5.set_node_attr('/time_data','sample_freq', fs)
acoularh5.close()
However, it raises a value error: ValueError: the shape ((0,)) and chunkshape ((256, 64)) ranks must be equal.
input from terminal:
python 2hf.py 01_83.wav" (python script.py relative-file-path)
Traceback error, please notice that in "environments/hdf/lib/python3.6/" "hdf" is the root folder of the virtual environment. "/tables/" is the folder for the package tables 3.6.1 (https://pypi.org/project/tables/) installed via the pip command in the virtual environment.
Traceback (most recent call last):
File "2hf.py", line 18, in <module>
byteorder=None, createparents=False, obj=data)
File "/home/giuseppe/environments/hdf/lib/python3.6/site-packages/tables/file.py", line 1384, in create_earray
track_times=track_times)
File "/home/giuseppe/environments/hdf/lib/python3.6/site-packages/tables/earray.py", line 160, in __init__
track_times)
File "/home/giuseppe/environments/hdf/lib/python3.6/site-packages/tables/carray.py", line 212, in __init__
(shape, chunkshape))
ValueError: the shape ((0,)) and chunkshape ((256, 64)) ranks must be equal.
Closing remaining open files:01_83.h5...done
I had the same error and solved it by changing the script this way
from scipy.io import wavfile
import tables
import numpy
import sys
#read data from wav
#fs, data = wavfile.read('/home/vittoria/Documents/corpus-test/01.wav')
fs, data = wavfile.read(sys.argv[1])
#ouput
folder=sys.argv[1][:-6]
name= sys.argv[1][-6:-3]+"h5"
#save_to acoular h5 format
acoularh5 = tables.open_file(folder+name, mode = "w", title = name)
acoularh5.create_earray('/','time_data', atom=None, title='', filters=None, \
expectedrows=100000, \
byteorder=None, createparents=False, obj=data)
acoularh5.set_node_attr('/time_data','sample_freq', fs)
acoularh5.close()
I basically just removed this part , chunkshape=[256,64] :-)
Hope this helped.

Resize image in Tkinter

So, I need to resize an image in tkinter. Before you do anything - this is not a duplicate. I have gone through every other question on this site and none have helped me. The thing with me is - I don't wan't to save the image. I need to load the image, resize it, then display it with PhotoImage in a label. I tried to use ImageTk, but for some reason it won't work.
Here's my code with ImageTk:
from tkinter import *
import PIL
from PIL import Image, ImageTk
root = Tk()
left_nose_path_white = Image.open(r'C:\Users\User\Documents\Python stuff\Other apps\Veteris\Dog photos\Advanced\Sides\GIF\Nose\Nose (left) - White.gif')
def resize_image():
global left_nose_path_white
total_screen_width = root.winfo_screenwidth()
total_screen_height = root.winfo_screenheight()
frame_width, frame_height = left_nose_path_white.size
dog_new_width = int(frame_width / 3)
dog_new_height = int(frame_height / 3)
left_nose_path_white = left_nose_path_white.resize((dog_new_width, dog_new_height), Image.LANCZOS)
resize_image()
left_nose_img_white = ImageTk.PhotoImage(file = left_nose_path_white)
label = Label(image = left_nose_img_white)
label.pack()
root.mainloop()
This returns the error:
File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\PIL\ImageTk.py", line 124, in __del__
name = self.__photo.name
AttributeError: 'PhotoImage' object has no attribute '_PhotoImage__photo'
My code should find the width and height of the original image, divide it by 3, and then show it. The reason I don't want to have to save the image is because the user will open the application several times.
I'm new to using PILL/Pillow, so the answer may be obvious.
I have Pillow installed using pip.
I only have one version of Python on my computer (Python 3.7)
Full Error:
Traceback (most recent call last):
File "C:\Users\User\Documents\Python stuff\Other apps\Veteris\Scripts\Veteris_program.py", line 230, in <module>
left_nose_img_white = ImageTk.PhotoImage(file = left_nose_path_white)
File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\PIL\ImageTk.py", line 95, in __init__
image = _get_image_from_kw(kw)
File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\PIL\ImageTk.py", line 64, in _get_image_from_kw
return Image.open(source)
File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\PIL\Image.py", line 2779, in open
prefix = fp.read(16)
AttributeError: 'Image' object has no attribute 'read'
Exception ignored in: <function PhotoImage.__del__ at 0x000002B0A8A49950>
Traceback (most recent call last):
File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\PIL\ImageTk.py", line 124, in __del__
name = self.__photo.name
AttributeError: 'PhotoImage' object has no attribute '_PhotoImage__photo'
Thanks for the help!

how to call Microsoft cognitive face and passing image as bytes python with cognitive_face

Hi im trying the same thing in this question
How can i pass capture image directly as a binary data for processing in API calling (Microsoft Cognitive Services) using Python
passing byte image to face detect library
but with the cognitive_face library
faces =CF.face.detect(buf.tobytes(),True,False,attributes='age,gender,emotion')
but im getting an error
Traceback (most recent call last): File ".\cam.py", line 80, in faces = CF.face.detect(buf.tobytes(),True,False,attributes='age,gender,headPose,smile>,facialHair,glasses,emotion,hair,makeup,occlusion,accessories,blur,exposure,n>oise') File "Python37\lib\site-packages\cognitive_face\face.py", line 33, in detect headers, data, json = util.parse_image(image) File "Python37\lib\site-packages\cognitive_face\util.py", line 133, in parse_image elif os.path.isfile(image): # When image is a file path. File "Python37\lib\genericpath.py", line 30, in isfile st = os.stat(path) ValueError: stat: embedded null character in path
You are using the old package named cognitive_face which unfortunately expects the input argument to be either a file name or a URL.
Fortunately, the new package name azure-cognitiveservices-vision-face supports streams, so if you switch over, you could do something like the following:
from azure.cognitiveservices.vision.face import FaceClient
from msrest.authentication import CognitiveServicesCredentials
import cv2
import os
face_key = '...' # your API key
face_endpoint = '...' # your endpoint, e.g. 'https://westus.api.cognitive.microsoft.com'
credentials = CognitiveServicesCredentials(face_key)
client = FaceClient(face_endpoint, credentials)
# img is your unencoded (raw) image, from the camera
img = ...
# buf will be the encoded image
ret,buf = cv2.imencode('.jpg', img)
# stream-ify the buffer
stream = io.BytesIO(buf)
# call the Face API
detected_faces = client.face.detect_with_stream(
stream,
return_face_id=True,
return_face_attributes=['age','gender','emotion'])
# access the response, example:
for detected_face in detected_faces:
print('{} happiness probability={}'.format(
detected_face.face_id,
detected_face.face_attributes.emotion.happiness))

Procedure on adding image pixel data in a file in newline?

import cv2
import numpy as np
import os
k=[]
file1=open("TextData.txt",'w')
fn=input("Enter filename : ")
img=cv2.imread(fn,cv2.IMREAD_GRAYSCALE)
l=len(img)
w=len(img[0])
print(str(l)+"\n"+str(w))
for i in range(len(img)):
for j in range(len(img[0])):
k.append(img[i,j])
for a in range(len[k]):
file1.write(str(k[a])+"\n")
file1.close()
Basically, I'm running into the error :
Traceback (most recent call last):
File "imagereads.py", line 17, in <module>
for a in range(len[k]):
TypeError: 'builtin_function_or_method' object is not subscriptable
I'm trying to write a program that will store each image data in a file and access that later on when needed. Can anyone help me in this ? I'm doing this so that I can directly use file1.readLines() to read each data later on.
At first I tried appending each element to k, converting to a string and storing it directly. But I'm having problems getting back the data from the file into a list. Any help on this matter too will be appreciated.

Writing pdf with pypdf2 gives error

I'm trying to write a simple script to merge two PDFs but have run into an issue when trying to save the output to disk. My code is
from PyPDF2 import PdfFileWriter, PdfFileReader
import tkinter as tk
from tkinter import filedialog
### Prompt the user for the 2 files to use via GUI ###
root = tk.Tk()
root.update()
file_path1 = tk.filedialog.askopenfilename(
filetypes=[("PDF files", "*.pdf")],
)
file_path2 = tk.filedialog.askopenfilename(
filetypes=[("PDF files", "*.pdf")],
)
###Function to combine PDFs###
output = PdfFileWriter()
def append_pdf_2_output(file_handler):
for page in range(file_handler.numPages):
output.addPage(file_handler.getPage(page))
#Actually combine the 2 PDFs###
append_pdf_2_output(PdfFileReader(open(file_path1, "rb")))
append_pdf_2_output(PdfFileReader(open(file_path2, "rb")))
###Prompt the user for the file save###
output_name = tk.filedialog.asksaveasfile(
defaultextension='pdf')
###Write the output to disk###
output.write(output_name)
output.close
The problem is that I get an error of
UserWarning: File to write to is not in binary mode. It may not be written to correctly. [pdf.py:453] Traceback (most recent call last): File "Combine2Pdfs.py", line 44, in output.write(output_name) File "/Library/Frameworks/Python.framework/Versions/3.5/lib/pytho‌​n3.5/site-packages/P‌​yPDF2/pdf.py", line 487, in write stream.write(self.header + b("\n")) TypeError: write() argument must be str, not bytes
Where have I gone wrong?
I got it by adding mode = 'wb' to tk.filedialog.asksaveasfile. Now it's
output_name = tk.filedialog.asksaveasfile(
mode = 'wb',
defaultextension='pdf')
output.write(output_name)
Try to use tk.filedialog.asksaveasfilename instead of tk.filedialog.asksaveasfile. You just want the filename, not the file handler itself.
###Prompt the user for the file save###
output_name = tk.filedialog.asksaveasfilename(defaultextension='pdf')

Resources