Kivy Camera speed? slow Performance - performance

The Code works as expected except for one thing. The picture from the camera only updates about every 15 seconds. I'm wondering if there is a way to get it it run a little faster. I'm using an acer switch tablet with an intel atom processor which doesn't have a lot of horsepower so I was wondering if that could be part of my issue or maybe I should try using OpenCV to try to get the picture. for now it doesn't need to record video or save images just a live stream is all it needs to do.
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.scrollview import ScrollView
from kivy.uix.screenmanager import ScreenManager,Screen
from kivy.base import runTouchApp
from kivy.uix.camera import Camera
import time
from kivy.uix.boxlayout import BoxLayout
class MainScreen(Screen):
pass
class OptionsScreen(Screen):
pass
class AlarmScreen(Screen):
pass
class Project_1App(App):
def build(self):
screenManager = ScreenManager()
screenManager.add_widget(MainScreen(name="main"))
screenManager.add_widget(OptionsScreen(name="options"))
screenManager.add_widget(AlarmScreen(name="alarm"))
return screenManager
Project_1App.run()
The kivy bit is here
<MainScreen>:
GridLayout:
rows:5
cols:1
padding: 10
spacing: 10
Camera:
index:0
resolution: (300,300)
play: True
Button:
text: "Options"
size_hint_y: None
width: 150
height: 50
on_release: root.manager.current = 'options'

Related

Changing the font of atom label in rdkit

I'm trying to change the font type of the atom labels in RDKIT.
The default font type is 'sans'. I ran this code on colab:
from rdkit import Chem
from rdkit.Chem.Draw import IPythonConsole
from rdkit.Chem import Draw
smiles = 'C1=CC=C(C=C1)I'
mol = [Chem.MolFromSmiles(smiles)]
IPythonConsole.drawOptions.atomLabelFontFace = 'Times New Roman'
pic = Draw.MolsToGridImage(mol, returnPNG=False, subImgSize= (256, 256), molsPerRow=1)
display (pic)
But it seems that the font type doesn't change, neither when I tried other font types. What's wrong?
Use IPythonConsole.drawOptions.fontFile to choose the font.
Times New Roman is not installed in Colab, so you have to install it or you can use the existing serif font (LiberationSerif-Regular.ttf).
from rdkit import Chem
from rdkit.Chem import rdDepictor, Draw
rdDepictor.SetPreferCoordGen(True)
from rdkit.Chem.Draw import IPythonConsole
IPythonConsole.molSize = (600, 300)
IPythonConsole.drawOptions.fontFile = r'/usr/share/fonts/truetype/liberation/LiberationSerif-Regular.ttf'
IPythonConsole.drawOptions.explicitMethyl = True
m = Chem.MolFromSmiles('CC(F)CC(Cl)C(C(=O)O)C(P)CC(S)CC(Br)CN') # Dummy molecule
rdDepictor.Compute2DCoords(m)
m
On your local computer you also need to set the path to the font:
IPythonConsole.drawOptions.fontFile = r'C:\Windows\Fonts\times.ttf'

Vertically center text inside label in Gtk using python

I'm trying to vertically center the text of a label inside a box. This is do code I'm trying:
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk
win = Gtk.Window()
win.set_default_size(200, 100)
box = Gtk.Box()
win.add(box)
lbl = Gtk.Label("FOO")
lbl.set_vexpand(True)
lbl.set_valign(Gtk.Align.CENTER)
# Set the background to make the problem visible
lbl.override_background_color(Gtk.StateFlags.NORMAL, Gdk.RGBA(red=1, green=0, blue=0))
box.add(lbl)
win.show_all()
win.connect('destroy', Gtk.main_quit)
Gtk.main()
As you can see, the label itself is centered perfectly fine inside the box, but the text inside the label is shifted slightly towards the top end of the label:
I'm not able to find anything about this. Programmatic as well as CSS-based solutions are highly appreciated.
I believe the problem is that you and the font creator have different opinions of what "centered" in the vertical sense means. Also think what will happen to the visual impression if you have characters like y and g. This will get even more confusing if you add international characters to the mix like Å or Ö.
Anyway, this answer uses CSS to create a configurable offset (padding-top), and will also give you freedom to change font. The 20 px value is obviously too much, but will give a clear visible evidence that it works.
style.css:
#custom_label {
padding-top: 20px;
background-color: red;
font: Vera 20px;
}
test.py:
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk
win = Gtk.Window()
win.set_default_size(200, 100)
box = Gtk.Box()
win.add(box)
lbl = Gtk.Label(u'FOO')
lbl.set_name('custom_label')
box.add(lbl)
style_provider = Gtk.CssProvider()
Gtk.StyleContext.add_provider_for_screen(Gdk.Screen.get_default(), style_provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)
with open("style.css") as f:
style_provider.load_from_data(f.read())
win.show_all()
win.connect('destroy', Gtk.main_quit)
Gtk.main()
As a bonus, if you start your program with:
GTK_DEBUG=interactive python test.py
(assuming Linux, not sure how to do this in Windows), you will have an interactive way to change the CSS and other widget properties.

Matploitlib image displaying at place of button

I have troubles using Matploitlib. My aim is to create program, which will be displaying image, with buttons allowing it's edition.
I started from the button allowing to pick an image, and I already have meet a problem. I wish image to load at the center of the window, but it loads at the place of button.
How to create a figure with fixed position, and how to choice it to display the image?
import matplotlib.pyplot as plt
from matplotlib.widgets import Button
import tkinter.filedialog as dialog
class Index(object):
def load(self, event):
filename = dialog.askopenfilename()
img = plt.imread(filename)
plt.imshow(img)
callback = Index()
axload = plt.axes([0.59, 0.05, 0.1, 0.075])
bload = Button(axload, 'Load')
bload.on_clicked(callback.load)
plt.show()
plt.close()
Okay, I found an answer by myself.
To create new axis
ax=plt.subplot(111)
and then, to use it instead of existing one, simply
ax.imshow(img)
instead of
plt.imshow(img)

when resizing a image set only resizing one image

When I was trying to resize an image set, it was only resizing the first image.How to resize all images? This is my code:
import numpy as np
import os
import cv2
pic_num = 1
img = cv2.imread("E:\ele/"+str(pic_num)+'.jpg',cv2.IMREAD_GRAYSCALE)
resized_image = cv2.resize(img,(100,100))
cv2.imwrite("E:\eye/"+str(pic_num)+'.jpg',resized_image)
pic_num += 1
If you are just looping through files and change it, and don't worried about time.
then you can just use for loop in python
For example you have pics from 1 too 100
Then you can just do following:
import numpy as np
import os
import cv2
for pic_num in range(1,100):
img = cv2.imread("E:\ele/"+str(pic_num)+'.jpg',cv2.IMREAD_GRAYSCALE)
resized_image = cv2.resize(img,(100,100))
cv2.imwrite("E:\eye/"+str(pic_num)+'.jpg',resized_image)

resize images in python

Can i resize images in python to given height and width,i use python 2.5, and i tried as this tutorial http://effbot.org/imagingbook/introduction.htm, and i installed PIL library for images,but when i try to write:
import Image
im = Image.open("test.jpg")
i got undefined variable from import:open
although import Imagedoesn't give errors?
Thanks in advance.
Your import appears to be the problem. Use this instead of "import Image":
from PIL import Image
Then go on like so:
image = Image.open('/example/path/to/image/file.jpg/')
image.thumbnail((80, 80), Image.ANTIALIAS)
image.save('/some/path/thumb.jpg', 'JPEG', quality=88)
To whom it may be of use: Just found that on the official Pillow website. You probably used Pillow and not PIL.
Warning
Pillow >= 1.0 no longer supports “import Image”. Please use “from PIL
import Image” instead.
This script resizes all images in a given folder:
import PIL
from PIL import Image
import os, sys
path = "path"
dirs = os.listdir( path )
def resize():
for item in dirs:
if os.path.isfile(path+item):
img = Image.open(path+item)
f, e = os.path.splitext(path+item)
img = img.resize((width,hight ), Image.ANTIALIAS)
img.save(f + '.jpg')
resize()
you can resize image using skimage
from skimage.transform import resize
import matplotlib.pyplot as plt
img=plt.imread('Sunflowers.jpg')
image_resized =resize(img, (244, 244))
plotting resized image
plt.subplot(1,2,1)
plt.imshow(img)
plt.title('original image')
plt.subplot(1,2,2)
plt.imshow(image_resized)
plt.title('image_resized')
for further code illustration : scikit-image
import os
from PIL import Image
imagePath = os.getcwd() + 'childFolder/myImage.png'
newPath = os.getcwd() + 'childFolder/newImage.png'
cropSize = 150, 150
img = Image.open(imagePath)
img.thumbnail(cropSize, Image.ANTIALIAS)
img.save(newPath)
if you have troubles with PIL the other alternative could be scipy.misc library. Assume that you want to resize to size 48x48 and your image located in same directory as script
from from scipy.misc import imread
from scipy.misc import imresize
and then:
img = imread('./image_that_i_want_to_resize.jpg')
img_resized = imresize(img, [48, 48])

Resources