Attribute error, cant use a imported library function in class object - framebuffer

from machine import Pin,SPI,PWM
import framebuf
import time
import os
import random
BL = 13
DC = 8
RST = 12
MOSI = 11
SCK = 10
CS = 9
class LCD_1inch3(framebuf.FrameBuffer): # For 320x240 display
framebuf =__import__('framebuf')
def __init__(self):
self.width = 320
self.height = 240
self.cs = Pin(CS,Pin.OUT)
self.rst = Pin(RST,Pin.OUT)
self.cs(1)
self.spi = SPI(1)
self.spi = SPI(1,1000_000)
self.spi = SPI(1,100000_000,polarity=0, phase=0,sck=Pin(SCK),mosi=Pin(MOSI),miso=None)
self.dc = Pin(DC,Pin.OUT)
self.dc(1)
self.buffer = bytearray(self.height * self.width * 2)
super().__init__(self.buffer, self.width, self.height, framebuf.RGB565)
self.init_display()
self.RED = 0x07E0
self.GREEN = 0x001f
self.BLUE = 0xf800
self.WHITE = 0xffff
self.BLACK = 0x0000
self.YELLOW= 0x07FF
def eyes2(self):
self.ellipse(10,10, 10, 10, LCD.BLACK)
I keep getting "AttributeError: 'LCD_1inch3' object has no attribute 'poly'" and idk y.
I can use the other ones in the framebuffer imported library like vline(), hline(), line(), rect(), fill() and text() but thats around it. Ive been trying to be able to use poly() and ellipse() but no success

Related

Pillow new RGB screen not generating

I built a test program using import PIL.imagedraw etc etc and was able to use the draw.line command to draw various lines. All good. In a new program I added new code which built a matrix list of x/y/z points (and cosine/sine expressions with math.lib), an input command for some variables, and then the draw.line command as in the previous code, but now the RGB window is not showing at all. The program doesn’t end and gives no errors. (Pythonista on iOS iPad Pro)
The test program, a noob attempt at perspective projection (which works as expected):
import PIL.ImageDraw as ImageDraw,PIL.Image as Image, PIL.ImageShow as ImageShow
im = Image.new("RGB", (1200,800))
draw = ImageDraw.Draw(im)
GL_z = 0
PP_y = 0
SP_x = 300
SP_y = -400
SP_z = 600
org=600
answer=0
width=100
depth=500
height=500
SP_z1=SP_z+height
px_1 = org
py_1 = org
px_2 = org+90
py_2 = 50 + org
px_3 = -160 + org
py_3 = 550 + org
px_4 = -250 + org
py_4 = 500+org
while answer==0:
SP_z1=SP_z-height
px1_1 = ((px_1 - SP_x)/(py_1 - SP_y))*(py_1 - PP_y)
px1_2 = ((px_2 - SP_x)/(py_2 - SP_y))*(py_2 - PP_y)
px1_3 = ((px_3 - SP_x)/(py_3 - SP_y))*(py_3 - PP_y)
px1_4 = ((px_4 - SP_x)/(py_4 - SP_y))*(py_4 - PP_y)
py1_1 = ((SP_z-GL_z)/(py_1-SP_y))*(py_1-PP_y)
py1_2 = ((SP_z-GL_z)/(py_2-SP_y))*(py_2-PP_y)
py1_3 = ((SP_z-GL_z)/(py_3-SP_y))*(py_3-PP_y)
py1_4 = ((SP_z-GL_z)/(py_4-SP_y))*(py_4-PP_y)
py1_5 = ((SP_z1-GL_z)/(py_1-SP_y))*(py_1-PP_y)
py1_6 = ((SP_z1-GL_z)/(py_2-SP_y))*(py_2-PP_y)
py1_7 = ((SP_z1-GL_z)/(py_3-SP_y))*(py_3-PP_y)
py1_8 = ((SP_z1-GL_z)/(py_4-SP_y))*(py_4-PP_y)
px1_1old = px1_1
px1_2old = px1_2
px1_3old = px1_3
px1_4old = px1_4
py1_1old = py1_1
py1_2old = py1_2
py1_3old = py1_3
py1_4old = py1_4
py1_5old = py1_5
py1_6old = py1_6
py1_7old = py1_7
py1_8old = py1_8
SP_yold = SP_y
draw.line((0,SP_y,1200,SP_y),fill=(255,255,255))
draw.line((px1_1,py1_1,px1_2,py1_2),fill=(0,0,255))
draw.line((px1_2,py1_2,px1_3,py1_3),fill=(0,255,0))
draw.line((px1_3,py1_3,px1_4,py1_4),fill=(255,255,255))
draw.line((px1_4,py1_4,px1_1,py1_1),fill=(100,255,0))
draw.line((px1_1,py1_1,px1_1,py1_5),fill=(255,255,255))
draw.line((px1_1,py1_5,px1_2,py1_6),fill=(255,255,255))
draw.line((px1_2,py1_6,px1_3,py1_7),fill=(255,255,255))
draw.line((px1_3,py1_7,px1_4,py1_8),fill=(255,255,255))
draw.line((px1_4,py1_8,px1_4,py1_4),fill=(150,150,0))
#draw.line((px1_1,py1_1,px1_2,py1_2),fill=(255,255,255))
#draw.line((px1_2,py1_2,px1_3,py1_3),fill=(255,255,255))
#draw.line((px1_3,py1_3,px1_4,py1_4),fill=(255,255,255))
#draw.line((px1_4,py1_4,px1_1,py1_1),fill=(255,255,255))
im.show()
move = input("''")
if move == "w":
SP_y += 50
if move == "s":
SP_x -= 50
if move == "z":
SP_y -= 50
if move == "a":
SP_x += 50
if move == "-":
SP_z -= 50
if move == "=":
SP_z += 50
draw.line((0,SP_yold,1200,SP_yold),fill=(0,0,0))
draw.line((px1_1old,py1_1old,px1_2old,py1_2old),fill=(0,0,0))
draw.line((px1_2old,py1_2old,px1_3old,py1_3old),fill=(0,0,0))
draw.line((px1_3old,py1_3old,px1_4old,py1_4old),fill=(0,0,0))
draw.line((px1_4old,py1_4old,px1_1old,py1_1old),fill=(0,0,0))
draw.line((px1_1old,py1_1old,px1_1old,py1_5old),fill=(0,0,0))
draw.line((px1_1old,py1_5old,px1_2old,py1_6old),fill=(0,0,0))
draw.line((px1_2old,py1_6old,px1_3old,py1_7old),fill=(0,0,0))
draw.line((px1_3old,py1_7old,px1_4old,py1_8old),fill=(0,0,0))
draw.line((px1_4old,py1_8old,px1_4old,py1_4old),fill=(0,0,0))
And this is the code that refuses to draw anything:
import math
import decimal
decimal.getcontext().prec = 6
import PIL.ImageDraw as ImageDraw,PIL.Image as Image, PIL.ImageShow as ImageShow
im = Image.new("RGB", (1200,800))
draw = ImageDraw.Draw(im)
PP_y = 0
SP_x = 300
SP_y = -400
SP_z = 600
org=600
answer=0
dims=[]
for i in range (1, 5):
dim = int(input())
dims.append(dim)
dims[3] = (dims[3]*math.pi)/180
obj1 = [[1,2,3],[4,5,6],[7,8,9],[10,11,12],[13,14,15],[16,17,18],[19,20,21],[22,23,24]]
y1 = [0,0,0,0,0,0,0,0]
y2 = [0,0,0,0,0,0,0,0]
y3 = [0,0,0,0,0,0,0,0]
x1 = [0,0,0,0,0,0,0,0]
z1 = [0,0,0,0,0,0,0,0]
ymod = [0,0,0,0,0,0,0,0]
xmod = [0,0,0,0,0,0,0,0]
obj1[0]=[0,0,0]
obj1[1]=[dims[0] * math.cos(dims[3]),dims[0] * math.sin(dims[3]),0]
obj1[2]=[obj1[1][0] - (dims[1] * math.sin(dims[3])),obj1[1][1] + (dims[1] * math.cos(dims[3])),0]
obj1[3]=[dims[1] * math.sin(dims[3]),dims[1] * math.cos(dims[3]),0]
obj1[4]=[0,0,dims[2]]
obj1[5]=[dims[0] * math.cos(dims[3]),dims[0] * math.sin(dims[3]),dims[2]]
obj1[6]=[obj1[1][0] - (dims[1] * math.sin(dims[3])),obj1[1][1] + (dims[1] * math.cos(dims[3])),dims[2]]
obj1[7]=[dims[1] * math.sin(dims[3]),dims[1] * math.cos(dims[3]),dims[2]]
for i in range (0,7):
y1[i]=obj1[i][1]-PP_y
y2[i]=obj1[i][1]-SP_y
y3[i]=y1[i]/y2[i]
x1[i]=obj1[i][0]-SP_y
z1[i]=obj1[i][2]-SP_z
obj1[i][0]=obj1[i][0] - (x1[i] * y3[i])
obj1[i][2]=obj1[i][2] - (z1[i] * y3[i])
#print(dims)
#print(obj1)
#print(y1,y2,y3,x1,z1,xmod,ymod)
while (True):
draw.line((obj1[0][0],obj1[0][2],obj1[1][0],obj1[1][2]),fill=(255,255,255))
Ah found the error, left out im.show(). Pretty fundamental…
reference
https://pillow.readthedocs.io/en/stable/reference/Image.html

ValueError: Target size (torch.Size([128])) must be the same as input size (torch.Size([112]))

I have a training function, in which inside there are two vectors:
d_labels_a = torch.zeros(128)
d_labels_b = torch.ones(128)
Then I have these features:
# Compute output
features_a = nets[0](input_a)
features_b = nets[1](input_b)
features_c = nets[2](inputs)
And then a domain classifier (nets[4]) makes predictions:
d_pred_a = torch.squeeze(nets[4](features_a))
d_pred_b = torch.squeeze(nets[4](features_b))
d_pred_a = d_pred_a.float()
d_pred_b = d_pred_b.float()
print(d_pred_a.shape)
The error raises in the loss function: ` pred_a = torch.squeeze(nets3)
pred_b = torch.squeeze(nets3)
pred_c = torch.squeeze(nets3)
loss = criterion(pred_a, labels_a) + criterion(pred_b, labels_b) + criterion(pred_c, labels) + d_criterion(d_pred_a, d_labels_a) + d_criterion(d_pred_b, d_labels_b)
The problem is that d_pred_a/b is different from d_labels_a/b, but only after a certain point. Indeed, when I print the shape of d_pred_a/b it istorch.Size([128])but then it changes totorch.Size([112])` independently.
It comes from here:
# Compute output
features_a = nets[0](input_a)
features_b = nets[1](input_b)
features_c = nets[2](inputs)
because if I print the shape of features_a is torch.Size([128, 2048]) but it changes into torch.Size([112, 2048])
nets[0] is a VGG, like this:
class VGG16(nn.Module):
def __init__(self, input_size, batch_norm=False):
super(VGG16, self).__init__()
self.in_channels,self.in_width,self.in_height = input_size
self.block_1 = VGGBlock(self.in_channels,64,batch_norm=batch_norm)
self.block_2 = VGGBlock(64, 128,batch_norm=batch_norm)
self.block_3 = VGGBlock(128, 256,batch_norm=batch_norm)
self.block_4 = VGGBlock(256,512,batch_norm=batch_norm)
#property
def input_size(self):
return self.in_channels,self.in_width,self.in_height
def forward(self, x):
x = self.block_1(x)
x = self.block_2(x)
x = self.block_3(x)
x = self.block_4(x)
# x = self.avgpool(x)
x = torch.flatten(x,1)
return x
I solved. The problem was the last batch. I used drop_last=True in the dataloader and It worked.

Python Lightning for sine wave simulation

I am getting an error when i run the below code. The error says
MisconfigurationException: No training_step() method defined. Lightning Trainer expects as minimum a training_step(), train_dataloader() and configure_optimizers() to be defined.
Can someone please let me know what is the issue here? I am very new to Pytorch. I am trying to simulate Sin wave using MLP
import numpy as np ## using again numpy library for Sin function
import torch ## using pytorch
import matplotlib.pyplot as plt
import pytorch_lightning as pl
import torch.optim as optim
from torch import nn
from pytorch_lightning import Trainer
from sklearn.model_selection import train_test_split
import pandas as pd
from torch.utils.data import DataLoader
N=1000 ## 1000 samples to be generated
L=1000 ## length of each sample
T=20 ## width of wave
x = np.random.randn(1000)
y = np.sin(x)
df = pd.DataFrame({'x':x, 'y':y})
train, test = train_test_split(df, test_size=0.2, random_state=42, shuffle=True)
target_fields=['y']
train_features, train_targets = train.drop(target_fields, axis=1), train[target_fields]
test_features, test_targets = test.drop(target_fields, axis=1), test[target_fields]
class MLP(pl.LightningModule):
def __init__(self):
super(MLP,self).__init__()
self.fc1 = nn.Linear(1, 10)
self.fc2 = nn.Linear(10, 1)
def forward(self, x):
x = torch.Relu(self.fc1(x))
x = self.fc2(x)
return x
l_rate = 0.2
mse_loss = nn.MSELoss(reduction = 'mean')
def train_dataloader(self):
train_dataset = TensorDataset(torch.tensor(train_features.values).float(), torch.tensor(train_targets[['cnt']].values).float())
train_loader = DataLoader(dataset = train_dataset, batch_size = 128)
return train_loader
def test_dataloader(self):
test_dataset = TensorDataset(torch.tensor(test_features.values).float(), torch.tensor(test_targets[['cnt']].values).float())
test_loader = DataLoader(dataset = test_dataset, batch_size = 128)
return test_loader
def configure_optimizers(self):
return optim.SGD(self.parameters(), lr=l_rate)
def training_step(self, batch, batch_idx):
x, y = batch
logits = self.forward(x)
loss = mse_loss(logits, y)
# Add logging
logs = {'loss': loss}
return {'loss': loss, 'log': logs}
def test_step(self, batch, batch_idx):
x, y = batch
logits = self.forward(x)
loss = mse_loss(logits, y)
correct = torch.sum(logits == y.data)
predictions_pred.append(logits)
predictions_actual.append(y.data)
return {'test_loss': loss, 'test_correct': correct, 'logits': logits}
def test_epoch_end(self, outputs):
avg_loss = torch.stack([x['test_loss'] for x in outputs]).mean()
logs = {'test_loss': avg_loss}
return {'avg_test_loss': avg_loss, 'log': logs, 'progress_bar': logs }
model = MLP()
trainer = Trainer(max_epochs = 50)
trainer.fit(model)
Error
GPU available: False, used: False
TPU available: False, using: 0 TPU cores
IPU available: False, using: 0 IPUs
---------------------------------------------------------------------------
MisconfigurationException Traceback (most recent call last)
<ipython-input-9-7bdf5ac9771f> in <module>()
1 model = MLP()
2 trainer = Trainer(max_epochs = 50)
----> 3 trainer.fit(model)
3 frames
/usr/local/lib/python3.7/dist-packages/pytorch_lightning/trainer/configuration_validator.py in __verify_train_loop_configuration(self, model)
50 if not has_training_step:
51 raise MisconfigurationException(
---> 52 "No `training_step()` method defined. Lightning `Trainer` expects as minimum a"
53 " `training_step()`, `train_dataloader()` and `configure_optimizers()` to be defined."
54 )
MisconfigurationException: No `training_step()` method defined. Lightning `Trainer` expects as minimum a `training_step()`, `train_dataloader()` and `configure_optimizers()` to be defined.
You are missing 2 parameters in your trainer.fit() call. See the documentation

Why tesseract wont find this easy text in image?

I have been trying for hours to resize and change color of this image but nothing is consistently getting correct letters. Please see image below. This is a test image I'm using. The goal is to use this for automation purposes. Thanks!
Larger sample
import numpy as np
import pytesseract
from PIL import ImageGrab
import win32gui
import time
toplist, winlist = [], []
#time.sleep(3)
def enum_cb(hwnd, results):
if 'FPS:' in win32gui.GetWindowText(hwnd):
print(hex(hwnd), win32gui.GetWindowText(hwnd))
winlist.append(hwnd)
win32gui.EnumWindows(enum_cb, None)
win32gui.SetForegroundWindow(winlist[0])
bbox = win32gui.GetWindowRect(winlist[0])
print(bbox)
img = np.array(ImageGrab.grab(bbox=(130, 810, 800, 1080)))
#percent by which the image is resized
scale_percent = 400
#calculate the 50 percent of original dimensions
width = int(img.shape[1] * scale_percent / 100)
height = int(img.shape[0] * scale_percent / 100)
# dsize
dsize = (width, height)
# resize image
output = cv2.resize(img, dsize)
#img.show()
pytesseract.pytesseract.tesseract_cmd = r"L:\Program Files\Tesseract-OCR\tesseract.exe"
# img = cv2.imread(r"L:\MGO2PC\MGO2 UNOFFICIAL PC\RPCS3 EMU\screenshots\screenshot-2021_04_06_17_13_03.png", 0)
#crop_img = img[800:900, 260:800]
#cv2.imshow("cropped", crop_img)
#cv2.waitKey(0)
i = cv2.imwrite("test.png",output)
text = pytesseract.image_to_string(output, lang='eng')
print(text)
Ultimately found that I can isolate the text color and tesseract had no problem reading after that.
def cv2_from_screen(self):
boundaries = [
([0, 179, 105], [38, 255, 167]) # BGR
]
pytesseract.pytesseract.tesseract_cmd = r"L:\Program Files\Tesseract-
OCR\tesseract.exe"
def enum_cb(hwnd, results):
if 'FPS:' in win32gui.GetWindowText(hwnd):
print(hex(hwnd), win32gui.GetWindowText(hwnd))
self.winlist.append(hwnd)
win32gui.EnumWindows(enum_cb, None)
win32gui.EnumWindows(enum_cb, None)
win32gui.SetForegroundWindow(self.winlist[0])
image = pyautogui.screenshot()
image = cv2.cvtColor(np.array(image.crop(box=[0, 800, 1000, 1080])), cv2.COLOR_RGB2BGR) #COLOR_RGB2BGR and COLOR_BGR2GRAY

Improve speed on joining multiple images

What I'm trying to do is take numerous(up to 48) 1024x768 images that are color coded images(weather maps, the precip overlay) and add up the precip to fall over the course of time. When I run into non-precip I want to take a box 5x5 around the pixel in question and average the value and use that value as the value of the pixel in question.
I can do this but it takes a long time to accomplish it. I have heard numpy could improve the speed but I still haven't been able to wrap my mind around how it going to improve the speed given the sequence of events that have to take place. It seems like I would still have to do it pixel by pixel. I've included an idea of the code I'm using to accomplish this SLOWLY.
I have this actually as two separate program, one to download the images and the other does the image processing(working up toward merging the two programs in the near future, just trying to get all the bugs worked out before the merger.) Hence some of the download coding may look a little strange. I figure I could probably write the file straight to a variable but I haven't been doing it that way so I stuck with a bit longer approach.
Is there anyway of increasing the speed? I don't see anyway of avoiding pixel by pixel due to the color coding scheme in place(look at the color bar in the lower left it shows the full color scheme...I only included part of it for demo purposes in the coding below.) Some of the coding may be a bit rough since I chopped from the two programs and put the important parts in here...it shows what I'm currently doing and gives the full idea of how I'm going about doing it.
Also, if you happen to see this three to four or more days after it was posted you would need to change the date in the download link to the current date. The files are only kept on the server for 3-4 days before they are removed.
from PIL import Image
import time
import urllib
import os
pathstr = '/'
url = 'http://mag.ncep.noaa.gov/GemPakTier/MagGemPakImages/gfs/20140216/00/gfs_namer_006_1000_500_thick.gif'
urllib.urlretrieve(url,str(pathstr + '20140216006.gif'))
url = 'http://mag.ncep.noaa.gov/GemPakTier/MagGemPakImages/gfs/20140216/00/gfs_namer_012_1000_500_thick.gif'
urllib.urlretrieve(url,str(pathstr + '20140216012.gif'))
url = 'http://mag.ncep.noaa.gov/GemPakTier/MagGemPakImages/gfs/20140216/00/gfs_namer_018_1000_500_thick.gif'
urllib.urlretrieve(url,str(pathstr + '20140216018.gif'))
url = 'http://mag.ncep.noaa.gov/GemPakTier/MagGemPakImages/gfs/20140216/00/gfs_namer_024_1000_500_thick.gif'
urllib.urlretrieve(url,str(pathstr + '20140216024.gif'))
class Convert():
def __init__(self):
self.colorscale2 = [(255,255,255),(127,255,0),(0,205,0),(145,44,238),(16,78,139),
(30,144,255),(0,178,238),(0,238,238),(137,104,205),(0,139,0),
(139,0,139),(139,0,0),(205,0,0),(238,64,0),(255,127,0),(205,133,0),
(255,215,0),(238,238,0),(255,255,0),(139,71,38),(255,0,0),(0,0,255),(0,0,0)]
self.x = 0
self.y = 0
self.grid = 0
self.moist = 0
self.scan = 0
self.turn = 0
self.precip = {}
start = time.time()
for i in range(6, 30, 6):
if i < 10:
filename = '/2014021600' + str(i) + '.gif'
else:
filename = '/201402160' + str(i) + '.gif'
self.im1 = Image.open(filename).convert('RGB')
self.image = self.im1.getdata()
self.size = width, height = self.im1.size
self.coordinates = self.x,self.y = width, height
self.getprecip()
self.turn = 1
print (time.time()-start)
def getprecip(self):
for self.x in range(81, 950):
for self.y in range(29, 749):
if self.turn == 0:
self.moist = 0
else:
self.moist = self.precip[self.x,self.y]
self.coordinates = self.x,self.y
self.scan = 0
self.imagescan()
if self.turn == 0:
self.precip[self.x,self.y] = self.moist
else:
self.precip[self.x,self.y] += self.moist
def imagescan(self):
if self.image[(self.y * 1024) + self.x] == self.colorscale2[0]:
self.moist =0
self.grid -=1
elif self.image[(self.y * 1024) + self.x] == self.colorscale2[1]:
self.moist =.01
elif self.image[(self.y * 1024) + self.x] == self.colorscale2[2]:
self.moist =.1
elif self.image[(self.y * 1024) + self.x] == self.colorscale2[3]:
self.moist =.25
elif self.image[(self.y * 1024) + self.x] == self.colorscale2[4]:
self.moist =.5
#on and on through self.colorscale2[18]
if self.scan == 1:
self.grid += 1
if self.scan == 0:
x = self.x
y = self.y
self.deliso540()
self.x = x
self.y = y
def deliso540(self):
self.grid = 1
self.scan = 1
for p in range(self.x-2,self.x+2):
for q in range(self.y-2,self.y+2):
self.x = p
self.y = q
self.imagescan()
self.moist = self.moist / self.grid