react 图像识别_无法在React中基于URL查找图像
react 图像识别
If you're new to React and are having trouble accessing images stored locally, you're not alone.
如果您不熟悉React,并且无法访问本地存储的图像,那么您并不孤单。
Imagine you have your images stored in a directory next to a component like this:
想象一下,您的图像存储在这样的组件旁边的目录中:
/src /components - component1 - component2 /img - img1 - img2
And you're trying to access the images in the
/imgdirectory from
component2:
而且您正在尝试从
component2访问
/img目录中的图像:
import React, { Component, useState, useEffect } from 'react'; import { render } from 'react-dom' import { useTransition, animated, config } from "react-spring"; import imgArr from './images'; import '../App.css'; const Slideshow = () => { const [index, set] = useState(0) const transitions = useTransition(imgArr[index], item => item.id, { from: { opacity: 0 }, enter: {opacity: 1 }, leave: { opacity: 0 }, config: config.molasses, }) useEffect(() => void setInterval(() => set(state => (state + 1) % 4), 2000), []) return transitions.map(({ item, props, key }) => ( <animated.div key={key} className="bg" style={{ ...props, slideshowContainerStyle}} > <img className="img" src={require(item.url)} alt=""/> </animated.div> )) } const slideshowContainerStyle = { width: '80%', height: '300px' } export default Slideshow;
You've tried the paths
../img/img1.jpgand
..img/img1.jpg, but get
Error: Cannot find module '<path>'.
您已经尝试了
../img/img1.jpg和
..img/img1.jpg路径,但得到
Error: Cannot find module '<path>'。
So what's going on here?
那么这是怎么回事?
关于create-react-app
(A little about create-react-app
)
Like most people, you probably used
create-react-appto bootstrap your project.
像大多数人一样,您可能使用了
create-react-app来引导您的项目。
In that case, using relative paths can be a bit tricky because
create-react-appbuilds the HTML, CSS, and JavaScript files to an output folder:
在这种情况下,使用相对路径可能会有些棘手,因为
create-react-app将HTML,CSS和JavaScript文件构建到输出文件夹中:
/public - index.html - bundle.js - style.css
There are a number of ways to use images with
create-react-app, but one of the easiest is to include your images into
/public. When your project is built,
/publicserves as the root directory.
通过
create-react-app有多种使用图像的方法,但是最简单的方法之一就是将图像包含在
/public。 构建项目时,
/public充当根目录。
You can read more about adding images or other assets in the Create React App docs.
您可以在Create React App文档中阅读有关添加图像或其他资产的更多信息。
汇入图片 (Importing images)
If you took a look at the docs, you'll notice that the code includes
importstatements to load assets like images and fonts.
如果您看一下文档,您会注意到该代码包含
import语句,以加载诸如图像和字体之类的资产。
Importing is useful when your asset, in this case an image, is in the same directory or near the component that uses it, and won't be used anywhere else. For example, if you have an input component with buttons that use SVGs for thumbs up and thumbs down icons.
当您的资产(在这种情况下为映像)位于使用该资产的目录中或该组件附近,并且在其他任何地方都不会使用时,导入很有用。 例如,如果您有一个带有按钮的输入组件,这些按钮使用SVG表示大拇指和大拇指向下的图标。
A bit advantage of using
importis that it will throw an error during build time if there's a typo. This sort of checking helps ensure users won't see a broken image that slipped by.
使用
import一个优点是,如果有错字,它将在构建期间引发错误。 这种检查有助于确保用户不会看到滑动的破损图像。
翻译自: https://www.freecodecamp.org/news/unable-to-find-images-based-on-url-in-react/
react 图像识别
- 图像处理之基于NCC模板匹配识别
- TensorFlow实操之--服装图像识别问题-基于Keras
- 基于图像识别的火灾探测技术
- 互联网相似图像识别检索引擎 —— 基于图像签名的方式
- 基于形状上下文算法的图像识别Demo初步OK,纪念一下
- 图像五值化与基于三值图像的车牌识别(1)
- 图像五值化与基于三值图像的车牌识别(4)
- 基于tensorflow + Vgg16进行图像分类识别的实验
- 请求WebService出现"因 URL 意外地以 结束,请求格式无法识别"解决方法
- 数字图像处理:基于MATLAB的车牌识别项目 标签: 图像处理matlab算法 2017-06-24 09:17 98人阅读 评论(0)
- 基于vue.js框架,在web展示图像识别分类的结果(Ubuntu环境下)
- 基于OpenCV的PHP图像人脸识别技术
- 基于数字图像处理的橘子识别系统
- 【图像识别】【读论文】基于Kinect手势识别的网页控制软件设计——陈建军
- WebService出现"因 URL 意外地以 结束,请求格式无法识别"的解决方法
- 基于深度残差网络的图像识别案例
- 自动人脸识别基本原理 --基于静态图像的识别算法(一)特征脸
- 基于matlab进行前期简单图像处理(灰度化,降噪,识别障碍物轮廓)
- 干货丨基于飞凌OK3399-C平台android系统下实现图像识别
- Matlab图像处理学习笔记(六):基于sift特征点的人民币识别