我知道有几个类似的问题,但我的问题对我来说是完全不同的.我有两个词典:
d1 = {'a': {'b': {'cs': 10}, 'd': {'cs': 20}}}
d2 = {'a': {'b': {'cs': 30}, 'd': {'cs': 20}}, 'newa': {'q': {'cs': 50}}}
Run Code Online (Sandbox Code Playgroud)
即d1有钥匙'a',并d2有钥匙'a'和'newa'(换句话说d1是我的旧词典,d2是我的新词典).
我想迭代这些字典,这样,如果键是相同的,检查它的值(嵌套字典),例如当我找到键'a'时d2,我将检查是否存在'b',如果是,则检查值'cs'(从更改10为30),如果这个值改变了我想要打印它.
另一种情况是,我想重点'newa'从d2为新添加的关键.
因此,在迭代这两个dicts后,这是预期的输出:
"d2" has new key "newa"
Value of "cs" is changed from 10 to 30 of key "b" which is of key "a"
Run Code Online (Sandbox Code Playgroud)
我有以下代码与我,我正在尝试许多循环,虽然不起作用,但也不是一个好的选项,因此我想找到是否可以用递归代码得到预期的输出. …
我有这个Dataframe:
import pandas as pd
df = pd.DataFrame({'Hugo' : {'age' : 21, 'weight' : 75},
'Bertram': {'age' : 45, 'weight' : 65},
'Donald' : {'age' : 75, 'weight' : 85}}).T
df.index.names = ['name']
age weight
name
Bertram 45 65
Donald 75 85
Hugo 21 75
Run Code Online (Sandbox Code Playgroud)
我想将索引更改为列'age':
df.set_index('age', inplace=True)
weight
age
45 65
75 85
21 75
Run Code Online (Sandbox Code Playgroud)
旧索引列名称丢失.有没有办法在不丢失原始索引列的情况下更改索引并再次将旧列作为"普通"列,这样看起来像这样?
name weight
age
45 Bertram 65
75 Donald 85
21 Hugo 75
Run Code Online (Sandbox Code Playgroud) 注意:使用Python的flyweight实现的一部分
import weakref
class CarModel:
_models = weakref.WeakValueDictionary()
def __new__(cls, model_name, *args, **kwargs):
model = cls._models.get(model_name)
if not model:
model = super().__new__(cls)
cls._models[model_name] = model
return model
def __init__(self, model_name, air=False):
if not hasattr(self, "initted"):
self.model_name = model_name
self.air = air
self.initted=True
Run Code Online (Sandbox Code Playgroud)
问题1>什么super()意思?这是否意味着父类CarModel?
问题2>我也很难理解该功能的__new__工作原理?具体来说,以下行.
model = super().__new__(cls)
Run Code Online (Sandbox Code Playgroud)
说明__new__:
构造函数被调用
__new__而不是__init__,并且只接受一个参数,即正在构造的类(在构造对象之前调用它,因此没有自参数).它还必须返回新创建的对象.
我已经看到OpenCV提供了一个基于LBP 直方图的分类器:
但我希望能够访问LBP直方图本身.例如:
histogram = calculate_LBP_Histogram( image )
Run Code Online (Sandbox Code Playgroud)
是否有任何功能在OpenCV中执行此操作?
我最近使用了tesseract OCR和python,当我尝试image_to_string从tesseract 导入时,我一直收到错误.
导致问题的代码:
# Perform OCR using tesseract-ocr library
from tesseract import image_to_string
image = Image.open('input-NEAREST.tif')
print image_to_string(image)
Run Code Online (Sandbox Code Playgroud)
上述代码导致的错误:
Traceback (most recent call last):
file "./captcha.py", line 52, in <module>
from tesseract import image_to_string
ImportError: cannot import name image_to_string
Run Code Online (Sandbox Code Playgroud)
我已经确认安装了tesseract模块:
digital_alchemy@roaming-gnome /home $ pydoc modules | grep 'tesseract'
Hdf5StubImagePlugin _tesseract gzip sipconfig
ORBit cairo mako tesseract
Run Code Online (Sandbox Code Playgroud)
我相信我已经抓住了所有必需的套餐,但不幸的是我只是陷入了困境.看来该功能不在模块中.
任何帮助非常感谢.
我按照blob检测示例(使用cv2.SimpleBlobDetector)并成功检测到二进制图像中的斑点.但后来我不知道如何提取关键点的坐标和面积.以下是blob检测的代码:
# I skipped the parameter setting part.
blobParams = cv2.SimpleBlobDetector_Params()
blobVer = (cv2.__version__).split('.')
if int(blobVer[0]) < 3:
detector = cv2.SimpleBlobDetector(blobParams)
else:
detector = cv2.SimpleBlobDetector_create(blobParams)
# Detect Blobs
keypoints_black = detector.detect(255-black_blob)
trans_blobs = cv2.drawKeypoints(gray_video_crop, \
keypoints_white, np.array([]), (0,0,255), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
Run Code Online (Sandbox Code Playgroud)
因此变量keypoints_black包含blob的信息.当我打印变量时,它看起来像这样(发现了2个blob):
KeyPoint 0x10b10b870, KeyPoint 0x10b1301b0
Run Code Online (Sandbox Code Playgroud)
那么如何获得关键点及其区域的质心坐标,以便我可以将它们作为osc消息发送以进行交互.
我正在编写一个从数据文件创建数组的方法.该方法如下:
import numpy
def readDataFile(fileName):
try:
with open(fileName, 'r') as inputs:
data = None
for line in inputs:
line = line.strip()
items = line.split('\t')
if data == None:
data = numpy.array(items[0:len(items)])
else:
data = numpy.vstack((data, items[0:len(items)]))
return numpy.array(data)
except IOError as ioerr:
print 'IOError: ', ioerr
return None
Run Code Online (Sandbox Code Playgroud)
我的数据文件包含数字行,每个数字都由一个标签相互分隔,例如:
1 2 3
4 5 6
7 8 9
Run Code Online (Sandbox Code Playgroud)
我希望收到如下数组:
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
Run Code Online (Sandbox Code Playgroud)
但是,结果包含dtype在最后:
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]], …Run Code Online (Sandbox Code Playgroud) 我有这段代码来检查变量在乘法时是否是Vector2我的Vector2类中的数字或 a 。
def __mul__(self, other):
match type(other):
case int | float:
pass
case Vector2:
pass
Run Code Online (Sandbox Code Playgroud)
如果我运行这个,我会得到SyntaxError: name capture 'int' makes remaining patterns unreachable,当我将鼠标悬停在 vscode 中时,它会给我:
"int" is not accessed
Irrefutable pattern allowed only as the last subpattern in an "or" pattern
All subpatterns within an "or" pattern must target the same names
Missing names: "float"
Irrefutable pattern is allowed only for the last case statement
Run Code Online (Sandbox Code Playgroud)
如果我删除 | float它仍然不起作用,所以我不能将它们分开。
我是OOP的新手.我的想法是实现以下类:
class name(object, name):
def __init__(self, name):
print name
Run Code Online (Sandbox Code Playgroud)
然后想法是创建该类的两个实例:
person1 = name("jean")
person2 = name("dean")
Run Code Online (Sandbox Code Playgroud)
我知道,这是不可能的,但是如何将输入参数传递给类的实例?
我正在使用skimage库进行大多数图像分析工作.
我有一个RGB图像,我打算提取texture喜欢的功能entropy,energy,homogeneity并contrast从图像.
以下是我正在执行的步骤:
from skimage import io, color, feature
from skimage.filters import rank
rgbImg = io.imread(imgFlNm)
grayImg = color.rgb2gray(rgbImg)
print(grayImg.shape) # (667,1000), a 2 dimensional grayscale image
glcm = feature.greycomatrix(grayImg, [1], [0, np.pi/4, np.pi/2, 3*np.pi/4])
print(glcm.shape) # (256, 256, 1, 4)
rank.entropy(glcm, disk(5)) # throws an error since entropy expects a 2-D array in its arguments
rank.entropy(grayImg, disk(5)) # given an output.
Run Code Online (Sandbox Code Playgroud)
我的问题是,从灰度图像(直接)计算的熵是否与从GLCM(纹理特征)中提取的熵特征相同?
如果没有,从图像中提取所有纹理特征的正确方法是什么?
注:我已经提到过:
python ×8
numpy ×2
opencv ×2
python-3.x ×2
arrays ×1
blob ×1
comparison ×1
dictionary ×1
entropy ×1
glcm ×1
keypoint ×1
ocr ×1
oop ×1
pandas ×1
python-3.10 ×1
scikit-image ×1
tesseract ×1
types ×1