I'm new to captcha decoding and I'm now trying to decode this.
I know, this is easy. But I can't make it.
I have come this far.
But still tesseract can't read this.
The output is 6385145(Yes,pretty close but this is the best result of 20 tries).
The script I'm using so far is.
from PIL import Image,ImageEnhance
import urllib,os,pdb
im1 = Image.open("code.png")
im1.save("orjinal.png","PNG")
width, height = im1.size
im2 = im1.crop((0, 0, width, height-11))
im2 = im2.resize((int(width*3), int(height*3)), Image.BICUBIC)
im2.save("code.png")
img = Image.open('code.png')
img = img.convert("RGBA")
datas = img.getdata()
yo = img.getpixel((1,1))
newData = []
for item in datas:
if item[0] == yo[0] and item[1] == yo[1] and item[2] == yo[2]:
newData.append((0, 0, 0, 255))
else:
newData.append(item)
img.putdata(newData)
img.save("captcha.png", "PNG")
os.remove("code.png")
os.system("tesseract -psm 8 captcha.png hi")
with open("hi.txt") as f:
print f.read()
I couldn't delete that white line so I cropped the image.
Other images/results:
Result: 5390?()
Result: 16009?
Related
I use opencv to count the number of white and black pixels of picture(I have convert them into black and white image), and everytime I run my code it return the number is 0,and the code is
output_path = "/content/drive/MyDrive/dataset_demo/result_pic"
for pic in os.listdir(output_path):
if pic.endswith('.jpg'):
image = cv2.imread(pic,cv2.IMREAD_UNCHANGED)
number_of_white_pix = np.sum(image == 255)
number_of_black_pix = np.sum(image == 0)
number_of_total = number_of_white_pix + number_of_black_pix
number_of_ratio = number_of_white_pix / number_of_black_pix
print(number_of_total)
The pic variable contains only the file name of the image, but cv2.imread needs the full path to the image in order to read it. You need to use the full path to the image when you call cv2.imread.
output_path = "/content/drive/MyDrive/dataset_demo/result_pic"
for pic in os.listdir(output_path):
if pic.endswith('.jpg'):
pic = os.path.join(output_path, pic) #full path to the image
image = cv2.imread(pic,cv2.IMREAD_UNCHANGED)
number_of_white_pix = np.sum(image == 255)
number_of_black_pix = np.sum(image == 0)
number_of_total = number_of_white_pix + number_of_black_pix
number_of_ratio = number_of_white_pix / number_of_black_pix
print(number_of_total)
I'm trying to write a program in Python that takes an image and replaces the pixels with images, based off of the pixels lightness value. Figured I'd start with following a tutorial on converting images to ASCII art, then I can replace the ASCII characters with images. Here's what this attempt looks like:
https://pastebin.com/VNFWd9xN
It's a bit quick and dirty, just to see if I can make it work, but I think you'll get the idea.
So, a couple of issues I ran into.
The first and biggest one, i get a "TypeError: sequence item 0: expected str instance, JpegImageFile found". I get that the program expects a string and gets an image instead. But, how do I solve that?
Lastly, more of a parenthesis really, but I was playing around with the save function and could not get it so save "ascii_image" to jpg.
Anyhow, would really appreciate some guidance here.
Thank you.
import PIL.Image
from PIL import Image
img1 = Image.open("image1.jpg")
img2 = Image.open("image2.jpg")
img3 = Image.open("image3.jpg")
img4 = Image.open("image4.jpg")
img5 = Image.open("image5.jpg")
img6 = Image.open("image6.jpg")
img7 = Image.open("image7.jpg")
img8 = Image.open("image8.jpg")
img9 = Image.open("image9.jpg")
img10 = Image.open("image10.jpg")
img11 = Image.open("image11.jpg")
img12 = Image.open("image12.jpg")
# ascii characters used to build the output text
#ASCII_CHARS = ["#", "#", "S", "%", "?", "*", "+", ";", ":", ",", "."]
ASCII_CHARS = [img1, img2, img3, img4, img5, img6, img8, img9, img10, img11, img12]
# resize image according to a new width
def resize_image(image, new_width=80):
width, height = image.size
ratio = height/width
new_height = int(new_width * ratio)
resized_image = image.resize((new_width, new_height))
return(resized_image)
# convert each pixel to greyscale
def grayify(image):
grayscale_image = image.convert("L")
return(grayscale_image)
# convert pixels to a string of ASCII characters
def pixels_to_ascii(image):
pixels = image.getdata()
characters = "".join([ASCII_CHARS[pixel//25] for pixel in pixels])
return(characters)
def main(new_width=80):
# attempt to open image from user-input
path = input("Enter path to image:\n")
try:
image = PIL.Image.open(path)
except:
print(path, " is not a valid pathname to an image")
return
# convert image to ASCII
new_image_data = pixels_to_ascii(grayify(resize_image(image)))
# format
pixel_count = len(new_image_data)
ascii_image = "\n".join([new_image_data[index:(index+new_width)] for index in range(0, pixel_count, new_width)])
# print result
print(ascii_image)
# save
#with Image.open("ascii_image.jpg") as f:
# f.write(ascii_image)
main()
I have the picture below used in Tesseract OCR:
My code to process the picture is:
# HOCR
with image[450:6200, 840:3550] as cropped:
imgPage = wi(image = cropped)
imageBlob = imgPage.make_blob('png')
horas = gerarHocr(imageBlob)
def gerarHocr(imageBlob):
image = Image.open(io.BytesIO(imageBlob))
markup = pytesseract.image_to_pdf_or_hocr(image, lang='por', extension='hocr', config='--psm 6')
soup = BeautifulSoup(markup, features='html.parser')
spans = soup.find_all('span', {'class' : 'ocrx_word'})
listHoras = []
...
return listHoras
Although my OCR is getting sometimes confused and duplicating 8 with 3 and returning 07:44/14:183 instead of 07:44/14:13 for example.
I think if I remove the grey lines using Wand I improve the confidence of the OCR.
How do I do that, please?
Thank you,
If the system is using ImageMagick-6, you can call Image.threshold(), but might need to remove the transparency first.
with Image(filename='PWILE.png') as img:
img.background_color = 'WHITE'
img.alpha_channel = False
img.threshold(threshold=0.5)
img.save(filename='output_threshold.png')
If you're using ImageMagick-7 (anything above version 7.0.8-41), then Image.auto_threshold() will work.
with Image(filename='support/PWILE.png') as img:
img.auto_threshold(method='otsu')
I would use cv2 and/or numpy.array
to convert light gray colors to white
img[ img > 128 ] = 255
to convert dark gray colors to black
img[ img < 128 ] = 0
import cv2
folder = '/home/user/images/'
# read it
img = cv2.imread(folder + 'old_img.png')
# convert ot grayscale
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# reduce colors
img[ img > 128 ] = 255
img[ img < 128 ] = 0
# save it
cv2.imwrite(folder + 'new_img.png', img)
# display result
#cv2.imshow('window', img)
#cv2.waitKey(0) # press any key in window to close it
#cv2.destroyAllWindows()
Result
Ok, so I've been having a problem with some tracking code(I'm not too skilled, please bear this in mind). It is meant to track objects(Juggling balls) of one colour and output a .csv data file.
import cv2
import numpy as np
#h,s,v range of the object to be tracked
h,s,v,h1,s1,v1 = 31,91,0,74,255,255#GREEN
#h,s,v,h1,s1,v1 = 0, 161, 52, 26 ,255, 255 #Orange
#h,s,v,h1,s1,v1 = 90, 37, 0, 143, 180, 255 #Blue
threshold_value = 0
output_path = ('C:\\Python27')
cap = cv2.VideoCapture('Users\Tyson\Desktop\MillsMess.mp4')
#takes an image, and a lower and upper bound
#returns only the parts of the image in bounds
def only_color(frame, (b,r,g,b1,r1,g1)):
# Convert BGR to HSV
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
# define range of blue color in HSV
lower = np.array([b,r,g])
upper = np.array([b1,r1,g1])
# Threshold the HSV image to get only blue colors
mask = cv2.inRange(hsv, lower, upper)
# Bitwise-AND mask and original image
res = cv2.bitwise_and(frame,frame, mask= mask)
return res, mask
#finds the largest contour in a list of contours
#returns a single contour
def largest_contour(contours):
c = max(contours, key=cv2.contourArea)
return c[0]
#takes an image and the threshold value returns the contours
def get_contours(im, threshold_value):
imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
_ ,thresh = cv2.threshold(imgray,threshold_value,255,0)
_, contours, _ = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
return contours
#finds the center of a contour
#takes a single contour
#returns (x,y) position of the contour
def contour_center(c):
M = cv2.moments(c)
try: center = int(M['m10']/M['m00']), int(M['m01']/M['m00'])
except: center = 0,0
return center
frame_number = 0
positions = []
for i in range(1000000): positions.append((0,0))
#main loop of the program
while True:
#read image from the video
_, img = cap.read()
try: l = img.shape
except: break
#extract only the flesh tones
img, mask = only_color(img, (h,s,v,h1,s1,v1))
#find the contours in the image
contours = get_contours(img, threshold_value)
#if there are contours found in the image:
if len(contours)>0:
try:
#sort the contours by area
c = max(contours, key=cv2.contourArea)
img = cv2.drawContours(img, c ,-1, (0,0,255), 14)
positions[frame_number] = contour_center(c)
except: pass
frame_number += 1
#show the image and wait
#cv2.imshow('img', img)
cv2.imshow('img', cv2.resize(img, (480,700)))
k=cv2.waitKey(1)
if k==27: break
#release the video to avoid memory leaks, and close the window
cap.release()
cv2.destroyAllWindows()
#remove unused parts of the list
positions = positions[:frame_number]
print 'finished tracking'
#write data
import csv
with open(output_path, 'w') as csvfile:
fieldnames = ['x_position', 'y_position']
writer = csv.DictWriter(csvfile, fieldnames = fieldnames)
writer.writeheader()
for position in positions:
x, y = position[0], position[1]
writer.writerow({'x_position': x, 'y_position': y})
print 'finished writing data'
print output_path
And I get this Error
finished tracking
Traceback (most recent call last):
File "C:\Users\Tyson\Desktop\Code\Tracker.py", line 90, in <module>
with open(output_path, 'w') as csvfile:
IOError: [Errno 13] Permission denied: 'C:\\Python27'
I have tried fixing it, but nothing seems to work. Is there a native folder I could save it to that is writable? Or how can I give permission?
Thanks in advance!
The line output_path = ('C:\\Python27') later causes open(output_path, 'w') to fail. Set output_path to something else.
i would like to do some program by capture image from webcam, then cropped it. after crop, i do some image processing and from the process it will run my robots. Here the full program:
import cv2
from cv2 import *
import numpy as np
import pylab
import pymorph
import mahotas
from matplotlib import pyplot
from PIL import Image
# initialize the camera
cam = VideoCapture(0) # 0 -> index of camera
s, img = cam.read()
# frame captured without any errors
if s:
imwrite("img.jpg",img) #save image
#Crop Image
imageFile = "img.jpg"
im1 = Image.open(imageFile)
def imgCrop(im):
box = (0, 199, 640, 200)
region = im.crop(box)
region.save('crop.jpg')
cImg = imgCrop(im1)
#thresholding
def greyImg(im):
gray = im.convert('L')
bw = gray.point(lambda x: 0 if x<128 else 255, '1')
bw.save("bw.jpg")
tImg = greyImg(cImg )
#direction
def find_centroid(im, rez):
width, height = im.size
XX, YY, count = 0, 0, 0
for x in xrange(0, width, rez):
for y in xrange(0, height, rez):
if im.getpixel((x, y)) == 255:
XX += x
YY += y
count += 1
return XX/count, YY/count
print find_centroid(tImg, 1)
def robo_direct():
cen = find_centroid(im, 1)
diff = cen[0] - 320
if diff > 10:
print 'right'
if diff < -10:
print 'left'
else:
print 'straight'
print robo_direct()
The error was come out like this:
File "compile.py", line 32, in greyImg
gray = im.convert('L')
AttributeError: 'NoneType' object has no attribute 'convert'
That is because im is a None object.
Try again the code with:
print im is None
And you'll see. I don't know about threshold, but obviously you are creating the im object the wrong way.
Your function imgCrop(im1) has no return statement and as such returns None. And then your greyImg(im) function also has no return statement and also will return None.
To fix that add return statements to both functions that for the first return region and the second return bw.
Also your robo_direct() function should return and not print the direction so that the call to it in the statement print robo_direct() would print the direction.