Can't download images with python 2.7 - python

I'm kinda new to Python. I want to download and rescale many images at once so I can use them as negative samples to train a haar cascade. Maybe I mixed some python 3.0 code in mine. I got the following error:
Traceback (most recent call last):
File "D:/!Servici/Python/Remake/negative_grab.py", line 26, in <module> store_raw_images()
File "D:/!Servici/Python/Remake/negative_grab.py", line 7, in store_raw_images
neg_image_urls = urllib2.urlopen(neg_images_link).read().decode()
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 4837: ordinal not in range(128)
Process finished with exit code 1
My code:
import urllib2
import cv2
import os
def store_raw_images():
neg_images_link = 'http://image-net.org/api/text/imagenet.synset.geturls?wnid=n04335209'
neg_image_urls = urllib2.urlopen(neg_images_link).read().decode()
if not os.path.exists('neg_sample'):
os.makedirs('neg_samples')
pic_num = 1
for i in neg_image_urls.split('\n'):
try:
print(i)
urllib2.urlretrieve(i, "neg/"+str(pic_num)+'.jpg')
img = cv2.imread("neg/"+str(pic_num)+'.jpg', cv2.IMREAD_GRAYSCALE)
resize_image = cv2.resize(img, (100,100))
cv2.imwrite(("neg/")+str(pic_num)+'.jpg', resize_image)
pic_num += 1
except Exception as e:
print (str(e))
store_raw_images()

Your decode is running into errors. Try with decode('utf-8')

Related

How to return value from C program to Python script

When I try to return value from c code to python code i got error.
Traceback (most recent call last):
File "python.py", line 54, in <module>
print("\n\n\n\RESULT: ", str(result, "utf-8"))
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xba in position 245: invalid start byte
in c function i try to return json string which got hex data - which I could parse in python and than make another calculation.
Example of returned string is "{"data":"0x123132"}"
In python i use
import ctypes
my_functions = ctypes.cdll.LoadLibrary("./my_functions.so")
my_functions.getJson.argtypes = (ctypes.c_char_p,)
my_functions.EthereumProcessor.restype = ctypes.c_char_p
result=my_functions.getJson()
print("\n\n\n\RESULT: ", str(result, "utf-8"))

Pyvisa decoding issue with Yokogawa oscilloscope

I am trying to read some values from an Yokogawa oscilloscope. I made a connection using pyvisa and it worked fine sending and receiving data from the osci. The issue appears when I try to read more than 857 values. If I set the END point 857 I can receive and print/write into a file the whole data but if I set it 858 I get the next error:
Traceback (most recent call last):
File "osci_connect.py", line 16, in <module>
values1 = (my_osci.query_ascii_values(':WAVEFORM:SEND? 0'))
File "C:\Python37\lib\site-packages\pyvisa\resources\messagebased.py", line 629, in query_ascii_values
delay)
File "C:\Python37\lib\site-packages\pyvisa\resources\messagebased.py", line 447, in read_ascii_values
block = self.read()
File "C:\Python37\lib\site-packages\pyvisa\resources\messagebased.py", line 413, in read
message = self._read_raw().decode(enco)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xec in position 8156: ordinal not in range(128)
and here is how the last bytes of data look like when I debug using pydev:
b'.04E+00,-0.04E+00,-0.02E+00,0.03E+00,-0.01E+00,-0.01E+00,0.00E+00,-0.02E+00,'
b'-0.01E+00,-0.03E+00,-0.03E+00,0.01E+00,0.04E+00,-0.01E+00,-0.02E+00,-0.06E+0'
b'0,0.02E+00,0.03E+00,0.03E+00,0.01E+00,0.04E+00,0.00E+00,0.01E+00,0.04E+00,0.'
b'03E+00,-0.03E+00,-0.\x00\x005\xc4\x1c\xbf~')
I'm guessing that the error is generated by the "\x00\x005..." characters but I don't understand why the others are returned as expected and after 857 I get this error.
Bellow is my code:
import visa
rm = visa.ResourceManager()
#rm = visa.ResourceManager('C:\WINDOWS\system32\visa32.dll')
my_osci = rm.open_resource("TCPIP::172.20.113.189::INSTR",write_termination='\n',read_termination='\n')
print("Hello, I am:" + my_osci.query("*IDN?"))
print("Send ':WAVEFORM:END 1' ")
my_osci.write(":WAVEFORM:FORMAT ASCII")
my_osci.write(":WAVEFORM:START 0")
my_osci.write(":WAVEFORM:END 858")
#values = my_osci.query_ascii_values(':WAVEFORM:END 20')
values1 = (my_osci.query_ascii_values(':WAVEFORM:SEND? 0'))
print("Received values:")
print(values1)

UnicodeEncodeError: trying to print win32_process (wmi module)

i'm trying to print all the process ( like task mannager), so i used
this code :
from ctypes import *
import wmi
c = wmi.WMI()
kernel32 = windll.kernel32
i = 0
txt = ""
for process in c.Win32_Process():
try:
txt = txt + str(process)
except:
print process
most of the process (about 144/146) pass the try and then i print all together but there are about 2 process that fail the try, and the except and raise this error while i'm trying to print them:
UnicodeEncodeError: 'ascii' codec can't encode characters in position 178-181: ordinal not in range(128)
someone know what to do?

Python 2 error:"Internal Python error in the inspect module

I have written this code to calculate the quadratic formula:
from numpy.lib.scimath import sqrt as csqrt
a = raw_input("a?")
b = raw_input("b?")
c = raw_input("c?")
def numcheck(x):
try:
i = float(x)
return True
except (ValueError, TypeError):
return False
if numcheck(a)==True:
a=int(a)
else:
print "a is not a number"
if numcheck(b)==True:
b=int(b)
else:
print "b is not a number"
if numcheck(c)==True:
c=int(c)
else:
print "b is not a number"
sqrt= ((b*b) - (4* (a*c)))
x_minus= (-b+(csqrt(sqrt)))/(2*a)
x_minus=str(x_minus)
x_plus= (-b-(csqrt(sqrt)))/(2*a)
x_plus=str(x_plus)
print "The solution is "+x_plus+" or "+x_minus
Never minding the rather crappy style, when the input is not a number, I get this error:
Traceback (most recent call last):
File "build\bdist.win32\egg\IPython\core\ultratb.py", line 776, in structured_traceback
File "build\bdist.win32\egg\IPython\core\ultratb.py", line 230, in wrapped
File "build\bdist.win32\egg\IPython\core\ultratb.py", line 267, in _fixed_getinnerframes
UnicodeDecodeError: 'ascii' codec can't decode byte 0xba in position 51: ordinal not in range(128)
ERROR: Internal Python error in the inspect module.
Below is the traceback from this internal error.
Unfortunately, your original traceback can not be constructed.
Can anyone tell me why this is happening and, if possible, a way to fix it? Thanks.
To do a sqrt, you can simply do this:
def sqrt(num):
return num ** 0.5
I'll try to give you more info related to your problem, but for now try to replace your sqrt with that one.

Python NLTK: SyntaxError: Non-ASCII character '\xc3' in file (Sentiment Analysis -NLP)

I am playing around with NLTK to do an assignment on sentiment analysis. I am using Python 2.7. NLTK 3.0 and NumPy1.9.1 version.
This is the code :
__author__ = 'karan'
import nltk
import re
import sys
def main():
print("Start");
# getting the stop words
stopWords = open("english.txt","r");
stop_word = stopWords.read().split();
AllStopWrd = []
for wd in stop_word:
AllStopWrd.append(wd);
print("stop words-> ",AllStopWrd);
# sample and also cleaning it
tweet1= 'Love, my new toyí ½í¸í ½í¸#iPhone6. Its good https://twitter.com/Sandra_Ortega/status/513807261769424897/photo/1'
print("old tweet-> ",tweet1)
tweet1 = tweet1.lower()
tweet1 = ' '.join(re.sub("(#[A-Za-z0-9]+)|([^0-9A-Za-z \t])|(\w+:\/\/\S+)"," ",tweet1).split())
print(tweet1);
tw = tweet1.split()
print(tw)
#tokenize
sentences = nltk.word_tokenize(tweet1)
print("tokenized ->", sentences)
#remove stop words
Otweet =[]
for w in tw:
if w not in AllStopWrd:
Otweet.append(w);
print("sans stop word-> ",Otweet)
# get taggers for neg/pos/inc/dec/inv words
taggers ={}
negWords = open("neg.txt","r");
neg_word = negWords.read().split();
print("ned words-> ",neg_word)
posWords = open("pos.txt","r");
pos_word = posWords.read().split();
print("pos words-> ",pos_word)
incrWords = open("incr.txt","r");
inc_word = incrWords.read().split();
print("incr words-> ",inc_word)
decrWords = open("decr.txt","r");
dec_word = decrWords.read().split();
print("dec wrds-> ",dec_word)
invWords = open("inverse.txt","r");
inv_word = invWords.read().split();
print("inverse words-> ",inv_word)
for nw in neg_word:
taggers.update({nw:'negative'});
for pw in pos_word:
taggers.update({pw:'positive'});
for iw in inc_word:
taggers.update({iw:'inc'});
for dw in dec_word:
taggers.update({dw:'dec'});
for ivw in inv_word:
taggers.update({ivw:'inv'});
print("tagger-> ",taggers)
print(taggers.get('little'))
# get parts of speech
posTagger = [nltk.pos_tag(tw)]
print("posTagger-> ",posTagger)
main();
This is the error that I am getting when running my code:
SyntaxError: Non-ASCII character '\xc3' in file C:/Users/karan/PycharmProjects/mainProject/sentiment.py on line 19, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details
How do I fix this error?
I also tried the code using Python 3.4.2 and with NLTK 3.0 and NumPy 1.9.1 but then I get the error:
Traceback (most recent call last):
File "C:/Users/karan/PycharmProjects/mainProject/sentiment.py", line 80, in <module>
main();
File "C:/Users/karan/PycharmProjects/mainProject/sentiment.py", line 72, in main
posTagger = [nltk.pos_tag(tw)]
File "C:\Python34\lib\site-packages\nltk\tag\__init__.py", line 100, in pos_tag
tagger = load(_POS_TAGGER)
File "C:\Python34\lib\site-packages\nltk\data.py", line 779, in load
resource_val = pickle.load(opened_resource)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xcb in position 0: ordinal not in range(128)
Add the following to the top of your file # coding=utf-8
If you go to the link in the error you can seen the reason why:
Defining the Encoding
Python will default to ASCII as standard encoding if no other
encoding hints are given.
To define a source code encoding, a magic comment must
be placed into the source files either as first or second
line in the file, such as:
# coding=

Categories

Resources